1

I am a beginner in flutter. The code i created is given below

import 'package:flutter/material.dart';

import './Text.dart';
import './TextSelect.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  // const ({ Key? key }) : super(key: key);

  @override
  State<StatefulWidget> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  var maps = {
    'country': ' My Favourite country is Netherland',
    'animal': 'My favourite animal is Tiger',
    'color': 'My favourite color is Blue'
  };
  var s=maps['country'];
  void indexPointer(String m) {
    setState(() {
      s = maps[m] as String;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text('This is it'),
            ),
            body: Column(children: [
              TextDisp(s),
              for (var k in maps.keys) TextSelec(k, () => indexPointer(k))
            ])));
  }
}
import 'package:flutter/material.dart';

@override
class TextSelec extends StatelessWidget {
  // const ({ Key? key }) : super(key: key);
  final String index;
  VoidCallback change;
  TextSelec(this.index, this.change);
  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: RaisedButton(child: Text(index), onPressed: change),
    );
  }
}
import 'package:flutter/material.dart';

class TextDisp extends StatelessWidget {
  // const ({ Key? key }) : super(key: key);
  //int m;
  final String disp;
  TextDisp(this.disp);
  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: Text(
        disp,
        textAlign: TextAlign.center,
        style: TextStyle(color: Colors.redAccent, fontSize: 30),
      ),
    );
  }
}

This code is showing two errors
The first is in the line var s= maps['country']; which says 'The instance member 'maps' can't be accessed in an initializer. Try replacing the reference to the instance member with a different expression'.
The second is in the line TextDisp(s) saying 'The argument type 'String?' can't be assigned to the parameter type 'String' '.
Can anyone tell why this error is appearing.

John George
  • 171
  • 2
  • 11

1 Answers1

1

First you have initialize this variable in init:

class MyAppState extends State<MyApp> {
  var maps = {
    'country': ' My Favourite country is Netherland',
    'animal': 'My favourite animal is Tiger',
    'color': 'My favourite color is Blue'
  };
  String? s;

  @override
  void initState() {
    s = maps['country'];  // <---- initialize it here
    super.initState();
  }

  void indexPointer(String m) {
    setState(() {
      s = maps[m] as String;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
            appBar: AppBar(
              title: Text('This is it'),
            ),
            body: Column(children: [
              TextDisp(s),
              for (var k in maps.keys) TextSelec(k, () => indexPointer(k))
            ])));
  }
}

Second should add the null aware operator for dart to check for you if that value is a String null:

TextDisp(s!),
Wilson Toribio
  • 990
  • 1
  • 4
  • 15
  • Can you explain why ' var s= maps['country'];' didn't work. – John George Apr 01 '22 at 15:47
  • 1
    Yes sure, the problem is that at that point of the program execution the maps variable has not being initialize so when you try to assing that variable to another variable then dart cannot see that it has a value, that is why you have to assing that variable inside initState which executes after all variables has been initialize. – Wilson Toribio Apr 01 '22 at 15:52