My code is working fine, but I don't know why the variable : Map<String, dynamic> dataJsonObject; return "null" when I build my app with adb (in android studio code). But then if I do a hot reload or a hot restart, my variable "dataJsonObject" return me the correct value. How do I get the correct value to return to me when I build the code the first time. Thank you
my code
import 'package:flutter/material.dart';
import 'dart:convert'; //(jsonDecode)
import 'package:flutter/services.dart'; // (loadJson)
import 'package:flutter/foundation.dart'; //(debugPrint)
class ProverbDisplay extends StatefulWidget {
final int myId;
final String myCountry;
const ProverbDisplay(this.myId, this.myCountry);
@override
_ProverbDisplayState createState() => _ProverbDisplayState();
}
class _ProverbDisplayState extends State<ProverbDisplay> {
Map<String, dynamic> dataJsonObject;
//Get JSON
Future getJsonProverb() async {
final String rawJson = await rootBundle.loadString('assets/json/data.json');
dataJsonObject = await jsonDecode(rawJson);
return dataJsonObject;
}
@override
void initState() {
super.initState();
getJsonProverb();
}
@override
Widget build(BuildContext context) {
debugPrint(' jsonData : $dataJsonObject'); //Return null
return Container(
color: Colors.blue,
height: 250,
child: Row(
children: [
Container(
width: 300,
child: PageView.builder(itemBuilder: (context, index) {
return Text("$dataJsonObject");
}),
),
],
),
);
}
}