Kimimomo
Kimimomo

Reputation: 105

Accessing data in an instance object in Flutter(Dart)

So I'm trying to create a list of a class that is pre-defined. The problem I'm having is to access the list of objects inside list. I want to eventually map these out and render them dynamically, but can you help tell me what I can improve in this particular lists or if I'm doing it correctly, can you give tell me if it is actually possible use the map method on it to make them render a widget? I have not made any widgets yet, but let's imaging I just use something simple like text. And yeah, there is only one instance right now but I will add more later on.

So the main question is:

How do I access the key value pair answers : text and score?

Again, if you think what I'm doing is weird and does not make sense, please feel free to correct me, I'm new to programming. Also, some helpful hints or any other hints or cheat sheets you can give me on lists and objects, and map methods would help tremendously because I've been struggling with this for weeks to get the advanced part if it down. I mean I get the basics, lists(a.k.a arrays) can be accessed by index, but then when you start mixing them with classes and objects inside objects inside lists I get so confused.

import 'package:flutter/material.dart';

import './testing.dart';

class MainQuiz {
  String question;
  var answer;

  MainQuiz(this.question, this.answer);
}

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

class MyApp extends StatelessWidget {
  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: MyStatefulWidget(),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  @override
  _MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> {
  var questionMap = [
    MainQuiz('What is the national Flower?', {
      'answers': [
        {'text': 'Rafflesia', 'score': 0},
        {'text': 'Rose', 'score': 0},
        {'text': 'Bunga Raya', 'score': 1},
        {'text': 'Bunga Telur', 'score': 0}
      ]
    })
  ];

  int number = 0;

  void changeNumHandler() {
    setState(() {
      number += 1;
    });
    print(questionMap[0]); //.map((qna){
    //     return
    //   })) = MainQuiz()
  }

  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Testing'),
      ),
      body: Center(child: Text('$number')),
      backgroundColor: Colors.blue.shade200,
      floatingActionButton: FloatingActionButton(
        onPressed: changeNumHandler,
        tooltip: 'Text Changer',
        child: const Icon(Icons.add),
      ),
    );
  }
}

Upvotes: 1

Views: 9471

Answers (2)

Alexis Olveres
Alexis Olveres

Reputation: 301

I have some releated problem, and i solve that making a method that returns the info i need. Just passing and id or position of the element.

So, with a method you can see more clear your code, and separete the dummy db to the implementation.

In resumen: You probably want to make every question as an instance of some class, for example Question, for more faster, secure and easy.

List <Question> _allDB = [
    Question(id: 01, text: "Some text", score: 1),
    Question(id: 02, text: "Some other text", score: 0),
]

getElement(id) {
    return [_allDB[id].text, _allDB[id].score]
}

Upvotes: 1

Kimimomo
Kimimomo

Reputation: 105

Nvm guys, I figured it out after a few minutes of tinkering here and there, did it like so:

var mappedQuestion = questionMap[0].answer['answers'].map((answer){
    return 'This is the ${answer['text']}, and it\'s score is ${answer['score']}';
    });

Upvotes: 2

Related Questions