Peter
Peter

Reputation: 79

Flutter / Dart / NoSuchMethodError

I have not been able to fix the error for 2 hours. Maybe one of you can help me. You can find a photo below.

What the Debug Console also tells me is:

[ The following NoSuchMethodError was thrown building MyApp (dirty, state: _MyAppState#c7f7e):
I/flutter (17681): The method 'map' was called on null.
I/flutter (17681): Receiver: null
I/flutter (17681): Tried calling: map<Answer>(Closure: (String) => Answer)]
import 'package:flutter/material.dart';

import './question.dart';
import './answer.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  var _questionIndex = 0;

  void _answerQuestion() {
    setState(() {
      _questionIndex = _questionIndex + 1;
    });
    print(_questionIndex);
  }

  @override
  Widget build(BuildContext context) {
    const questions = const [
      {
        'questionText': 'What\'s your favorite color?',
        'answer': ['Black', 'Yellow', 'white', 'green'],
      },
      {
        'questionText': 'What\'s your favorite animal?',
        'answer': ['Lion', 'Rabbit', 'Snake', 'Elephant'],
      },
      {
        'questionText': 'What\'s your favorite instructor?',
        'answer': ['Flo', 'Viona', 'Flint', 'Flo'],
      },
    ];

    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('My First App!'),
        ),
        body: Column(
          children: [
            Question(
              questions[_questionIndex]['questionText'],
            ),
            ...(questions[_questionIndex]['answers'] as List<String>)
                .map((answer) {
              return Answer(_answerQuestion, answer);
            }).toList()
          ],
        ),
      ),
    );
  }
}

import 'package:flutter/material.dart';

//import './main.dart';

class Question extends StatelessWidget {
  final String questionText;

  Question(this.questionText);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      margin: EdgeInsets.all(10),
      child: Text(
        questionText,
        style: TextStyle(fontSize: 28),
        textAlign: TextAlign.center,
      ),
    );
  }
}

import 'package:flutter/material.dart';

class Answer extends StatelessWidget {
  final Function selectHandler;
  final String answerText;

  Answer(this.selectHandler, this.answerText);

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: RaisedButton(
        color: Colors.blue,
        textColor: Colors.white,
        child: Text(answerText),
        onPressed: selectHandler,
      ),
    );
  }
}

That shows me the app

Upvotes: 0

Views: 759

Answers (2)

Ankit Jain
Ankit Jain

Reputation: 1886

Its due to higher Flutter version you are using. Do the change in pubspec.yaml file. Change the version to 2.11.0

environment:
  sdk: ">=2.11.0 <3.0.0"

Upvotes: 0

julemand101
julemand101

Reputation: 31209

There are no key called answers in your map. Instead it is called answer. So questions[_questionIndex]['answers'] will return null since the [] operator for Map is documented as:

Returns the value for the given key or null if key is not in the map.

https://api.dart.dev/stable/2.8.3/dart-core/Map/operator_get.html

Upvotes: 1

Related Questions