I receive this error when ever i try to fetch data from json file. I don't know what to next. there are two errors i am receiving : 1 is : The method '[]' was called on null. Receiver: null Tried calling:
and the second error that i am getting is: The following _TypeError was thrown building FutureBuilder(dirty, state: _FutureBuilderState#43e62): type 'Null' is not a subtype of type 'Map<String, dynamic>' to check what is the output i printed the array and this is i am getting:
I/flutter (14372): {
I/flutter (14372): "questions": [
I/flutter (14372): {
I/flutter (14372): "1": "Match the shape by clicking on all the shapes that are the same as the one you just saw ?"
I/flutter (14372): },
I/flutter (14372): {
I/flutter (14372): "2": "Match the shape by clicking on all the shapes that are the same as the one you just saw 2?"
I/flutter (14372): },
I/flutter (14372): {
I/flutter (14372): "3": "Match the shape by clicking on all the shapes that are the same as the one you just saw 4?"
I/flutter (14372): },
I/flutter (14372): {
I/flutter (14372): "4": "Match the shape by clicking on all the shapes that are the same as the one you just saw ?"
I/flutter (14372): },
I/flutter (14372): {
I/flutter (14372): "5": "Match the shape by clicking on all the shapes that are the same as the one you just saw ?"
I/flutter (14372): },
I/flutter (14372): {
I/flutter (14372): "6": "Match the shape by clicking on all the shapes that are the same as the one you just saw ?"
I/flutter (14372): }
I/flutter (14372): ],
I/flutter (14372): "options": [
I/flutter (14372): {
I/flutter (14372): "1": [{
I/flutter (14372): "a":"oo","b":"aaa","c":"www","d":"op"
I/flutter (14372): }]
I/flutter (14372): },
I/flutter (14372): {
I/flutter (14372): "2": [{"a":"ee","b":"ww","c":"ww","d"
this is the quizpage:
class Getjson extends StatelessWidget {
const Getjson({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: DefaultAssetBundle.of(context)
.loadString("assets/files/dyslexQ.json"),
builder: (context, snapshot) {
//print(snapshot.data.toString());
Map<String, dynamic> mydata = json.decode(snapshot.data.toString());
// ignore: unnecessary_null_comparison
if (mydata == null) {
return Scaffold(
body: Center(
child: Text("Loading"),
),
);
} else {
print(snapshot.data.toString());
return quizpage(mydata: mydata);
}
},
);
}
}
// ignore: camel_case_types, must_be_immutable
class quizpage extends StatefulWidget {
// ignore: prefer_typing_uninitialized_variables
var mydata;
quizpage({Key? key, required this.mydata}) : super(key: key);
@override
// ignore: no_logic_in_create_state
State<quizpage> createState() => _quizpageState(mydata);
}
// ignore: camel_case_types
class _quizpageState extends State<quizpage> {
// ignore: prefer_typing_uninitialized_variables
var mydata;
_quizpageState(this.mydata);
void checkanswer(String k) {}
Widget choicebutton(String k) {
return Padding(
padding: EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
child: MaterialButton(
onPressed: () {
Colors.blue;
},
child: Text(
mydata[1]["1"][k],
style: TextStyle(
color: Colors.black,
fontSize: 16.0,
),
maxLines: 1,
),
splashColor: Colors.blue,
highlightColor: Color.fromARGB(255, 22, 93, 152),
color: Colors.cyan,
minWidth: 200.0,
height: 45.0,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
),
);
}
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitDown, DeviceOrientation.portraitUp]);
return Scaffold(
body: Column(
children: <Widget>[
Expanded(
flex: 3,
child: Container(
padding: EdgeInsets.all(15.0),
alignment: Alignment.bottomLeft,
child: Text(
mydata[1]["1"],
style: TextStyle(fontSize: 16.0, fontFamily: "GoogleFont"),
),
),
),
Expanded(
flex: 6,
// ignore: avoid_unnecessary_containers
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
choicebutton("a"),
choicebutton("b"),
choicebutton("c"),
choicebutton("d"),
],
)),
),
Expanded(
flex: 1,
child: Container(
alignment: Alignment.topCenter,
child: Center(
child: Text(
"30",
style: TextStyle(
fontSize: 35.0,
fontWeight: FontWeight.w700,
fontFamily: 'Times New Roman',
),
),
),
),
),
],
),
);
}
}
this is my json file:
{
"questions": [
{
"1": "Match the shape by clicking on all the shapes that are the same as the one you just saw ?"
},
{
"2": "Match the shape by clicking on all the shapes that are the same as the one you just saw 2?"
},
{
"3": "Match the shape by clicking on all the shapes that are the same as the one you just saw 4?"
},
{
"4": "Match the shape by clicking on all the shapes that are the same as the one you just saw ?"
},
{
"5": "Match the shape by clicking on all the shapes that are the same as the one you just saw ?"
},
{
"6": "Match the shape by clicking on all the shapes that are the same as the one you just saw ?"
}
],
"options": [
{
"1": [{
"a":"oo","b":"aaa","c":"www","d":"op"
}]
},
{
"2": [{"a":"ee","b":"ww","c":"ww","d":"qq"
}]
},
{
"3": [{"a":"qq","b":"ff","c":"ff","d":"ew"
}]
},
{
"4":[ {"a":"few","b":"e","c":"ew","d":"ew"
}]
},
{
"5":[ {"a":"few","b":"few","c":"ew","d":"ef"
}]
},
{
"6":[ {"a":"wq","b":"wqqw","c":"qwdw","d":"qwqd"
}]
}
]
}
CodePudding user response:
jsonDecode('') and not json.Decode('')
create a main() to run your stateless widget. I hope this code solves your problem.
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const Getjson());
}
class Getjson extends StatelessWidget {
const Getjson({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: DefaultAssetBundle.of(context)
.loadString("assets/files/dyslexQ.json"),
builder: (context, snapshot) {
//print(snapshot.data.toString());
Map<String, dynamic> mydata = jsonDecode(snapshot.data.toString());
// ignore: unnecessary_null_comparison
if (mydata == null) {
return const Scaffold(
body: Center(
child: Text("Loading"),
),
);
} else {
print(snapshot.data.toString());
return QuizPage(mydata: mydata);
}
},
);
}
}
// ignore: camel_case_types, must_be_immutable
class QuizPage extends StatefulWidget {
// ignore: prefer_typing_uninitialized_variables
var mydata;
QuizPage({Key? key, required this.mydata}) : super(key: key);
@override
// ignore: no_logic_in_create_state
State<QuizPage> createState() => _QuizPageState(mydata);
}
// ignore: camel_case_types
class _QuizPageState extends State<QuizPage> {
// ignore: prefer_typing_uninitialized_variables
var mydata;
_QuizPageState(this.mydata);
void checkAnswer(String k) {}
Widget choiceButton(String k) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 10.0, horizontal: 20.0),
child: MaterialButton(
onPressed: () {
Colors.blue;
},
child: Text(
mydata[1]["1"][k],
style: const TextStyle(
color: Colors.black,
fontSize: 16.0,
),
maxLines: 1,
),
splashColor: Colors.blue,
highlightColor: const Color.fromARGB(255, 22, 93, 152),
color: Colors.cyan,
minWidth: 200.0,
height: 45.0,
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(20.0)),
),
);
}
@override
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
return Scaffold(
body: Column(
children: <Widget>[
Expanded(
flex: 3,
child: Container(
padding: const EdgeInsets.all(15.0),
alignment: Alignment.bottomLeft,
child: Text(
mydata[1]["1"],
style: const TextStyle(fontSize: 16.0, fontFamily: "GoogleFont"),
),
),
),
Expanded(
flex: 6,
// ignore: avoid_unnecessary_containers
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
choiceButton("a"),
choiceButton("b"),
choiceButton("c"),
choiceButton("d"),
],
)),
),
Expanded(
flex: 1,
child: Container(
alignment: Alignment.topCenter,
child: const Center(
child: Text(
"30",
style: TextStyle(
fontSize: 35.0,
fontWeight: FontWeight.w700,
fontFamily: 'Times New Roman',
),
),
),
),
),
],
),
);
}
}
CodePudding user response:
You need to check the state of the Future because you attempted to use snapshot.data
without checking it's null or not
if(snapshot.connectionState == ConnectionState.waiting){
// Wait the future, data is not ready yet...
// SizedBox.shrink(); is just for sampling, fit return statement by your purpose
return SizedBox.shrink();
} else {
if(snapshot.hasError){
// Did you know ? Something bad happened...
return SizedBox.shrink();
} else {
// All is well! Use me
Map<String, dynamic> mydata = json.decode(snapshot.data.toString());
}
}