sleonhart
sleonhart

Reputation: 15

Flutter : I can't call the key value of my map (Object) into String

I am following a tutorial and now stuck because I can't call the key value.

Here is my main.dart script :

import 'package:flutter/material.dart';
import 'package:first_app/question.dart';
import './answer.dart';


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

class MyApps extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return MyAppsState();
  }
}

class MyAppsState extends State<MyApps> {
  int indexQuestion = 0;

  void answerQuestion() {
    setState(() {
      indexQuestion += 1;
    });
    if (indexQuestion > 1) {
      indexQuestion = 1;
    }
    print('add index by 1');
  }

  void resetIndex() {
    setState(() {
      indexQuestion = 0;
    });
    print('Reset to 0');
  }

  var a = 'as';

  Widget build(BuildContext context) {
    var questions = [
      {
        'questionText': 'What\'s your favorite animal?',
        'answers': ['cat', 'dog', 'elephant', 'fish'],
      },
      {
        'questionText': 'What\'s your favorite color?',
        'answers': ['red', 'green', 'blue', 'black'],
      },
      {
        'questionText': 'Who\'s your favorite actor?',
        'answers': ['D\'Caprio', 'Tony Hawk', 'Scarlet', 'J-Low'],
      },
    ];

    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('My First App Yo!'),
          ),
          body: Column(
            children: [
// ========================ERROR HERE========================
              Question(
                questions[indexQuestion]['questionText'],
              ),
//===========================================================
              Text('indexQuestion = $indexQuestion'),
              Answer(answerQuestion),
              Answer(answerQuestion),
              RaisedButton(
                child: Text('Answer 3'),
                onPressed: () {
                  print('Anonymus function is pressed!');
                },
              ),
              RaisedButton(
                child: Text('Answer 4'),
                onPressed: answerQuestion,
              ),
              RaisedButton(
                child: Text('Reset'),
                onPressed: resetIndex,
              ),
              Container(
                color: Colors.amber,
                width: 50,
                height: 50,
                margin: EdgeInsets.all(10),
              ),
              Center(
                child: SetGo(),
              ),
            ],
          )),
    );
  }
}

Here is the question.dart file that need the argument at the Question line :

    import 'package:flutter/material.dart';

class Question extends StatelessWidget {
  final String questionText;

  Question(this.questionText);

  // Question({String questionTextoption}) {
  //   questionText = questionTextoption;
  // }

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

The error is at this line :

Question(
                questions[indexQuestion]['questionText'],
              ),

And here is the error : The argument type 'Object' can't be assigned to the parameter type 'String'. enter image description here

That value is going to be called as String in other function in other file. Can anybody help me to solve this please?

Upvotes: 1

Views: 719

Answers (1)

bluenile
bluenile

Reputation: 6029

Your given code is working.

enter image description here

import 'package:flutter/material.dart';
//import 'package:first_app/question.dart';
//import './answer.dart';


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

class MyApps extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return MyAppsState();
  }
}

class MyAppsState extends State<MyApps> {
  int indexQuestion = 0;

  void answerQuestion() {
    setState(() {
      indexQuestion += 1;
    });
    if (indexQuestion > 1) {
      indexQuestion = 1;
    }
    print('add index by 1');
  }

  void resetIndex() {
    setState(() {
      indexQuestion = 0;
    });
    print('Reset to 0');
  }

  var a = 'as';

  Widget build(BuildContext context) {
    var questions = [
      {
        'questionText': 'What\'s your favorite animal?',
        'answers': ['cat', 'dog', 'elephant', 'fish'],
      },
      {
        'questionText': 'What\'s your favorite color?',
        'answers': ['red', 'green', 'blue', 'black'],
      },
      {
        'questionText': 'Who\'s your favorite actor?',
        'answers': ['D\'Caprio', 'Tony Hawk', 'Scarlet', 'J-Low'],
      },
    ];

    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text('My First App Yo!'),
          ),
          body: Column(
            children: [
// ========================ERROR HERE========================
              Question(
                questions[indexQuestion]['questionText'],
              ),
//===========================================================
              Text('indexQuestion = $indexQuestion'),
              //Answer(answerQuestion),
              //Answer(answerQuestion),
              RaisedButton(
                child: Text('Answer 3'),
                onPressed: () {
                  print('Anonymus function is pressed!');
                },
              ),
              RaisedButton(
                child: Text('Answer 4'),
                onPressed: answerQuestion,
              ),
              RaisedButton(
                child: Text('Reset'),
                onPressed: resetIndex,
              ),
              Container(
                color: Colors.amber,
                width: 50,
                height: 50,
                margin: EdgeInsets.all(10),
              ),
              Center(
                //child: SetGo(),
              ),
            ],
          )),
    );
  }
}



class Question extends StatelessWidget {
  final String questionText;

  Question(this.questionText);

  // Question({String questionTextoption}) {
  //   questionText = questionTextoption;
  // }

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

Upvotes: 0

Related Questions