The instance member 'maps' can't be accessed in an initializer Error in flutter

Viewed 106

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.

1 Answers

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!),
Related