Skip to content

Instantly share code, notes, and snippets.

@Blasanka
Last active May 1, 2020 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Blasanka/212d4a874dd25c6324da5a7e14c6649a to your computer and use it in GitHub Desktop.
Save Blasanka/212d4a874dd25c6324da5a7e14c6649a to your computer and use it in GitHub Desktop.
This is a example to interate over nested list and access map inside it and render values on it.
import 'dart:convert';
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(),
),
),
);
}
}
class MyWidget extends StatelessWidget {
var questions = [
{
'question': '1+1',
'answer': [
{'text': 2,'correct':1},
{'text': 3,'correct':0},
{'text': 4,'correct':0},
{'text': 5,'correct':0}
]
},
{
'question': '1+2',
'answer': [
{'text': 2,'correct':0},
{'text': 3,'correct':1},
{'text': 4,'correct':0},
{'text': 5,'correct':0}
]
},
{
'question': '1+3',
'answer': [
{'text': 2,'correct':0},
{'text': 3,'correct':0},
{'text': 4,'correct':1},
{'text': 5,'correct':0}
]
},
{
'question': '1+4',
'answer': [
{'text': 2,'correct':0},
{'text': 3,'correct':0},
{'text': 4,'correct':0},
{'text': 5,'correct':1}
]
},
{
'question': '1+6',
'answer': [
{'text': 7,'correct':1},
{'text': 8,'correct':0},
{'text': 9,'correct':0},
{'text': 10,'correct':0}
]
},
{
'question': '1+6',
'answer': [
{'text': 7,'correct':1},
{'text': 8,'correct':0},
{'text': 9,'correct':0},
{'text': 10,'correct':0}
]
}, {
'question': '1+7',
'answer': [
{'text': 7,'correct':0},
{'text': 8,'correct':1},
{'text': 9,'correct':0},
{'text': 10,'correct':0}
]
},
];
@override
Widget build(BuildContext context) {
return ListView.builder(
itemCount: questions.length,
itemBuilder: (BuildContext context, int index) {
final quest = questions[index]['answer'];
return Row(
children: [
for (final q in quest)
RaisedButton(
onPressed: (){
print(q["correct"] == 1);
},
child: Text(q["text"].toString() ?? ""),
)
],
);
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment