I'm trying to load a json file into my main() app method. Can you tell me if this is possible? I've tryed File and rootBundle but it seems that Assets' folder are not ready yet.
here is my code:
ASSETS
assets:
- assets/settings/settings.json
MAIN METHOD
void main() async {
final file = await rootBundle.loadString('assets/settings/settings.json');
final data = jsonDecode(file);
Settings settings = Get.put(Settings.fromJson(data), permanent: true);
runApp(MyApp());
}
Found the Solution using FutureBuilder
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
_load() async {
final file = await rootBundle.loadString('assets/j.json');
final data = await jsonDecode(file);
print(data.toString());
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: _load(),
builder: (_, AsyncSnapshot<dynamic> snapshot) {
return !snapshot.hasData
? Container(
color: Colors.white,
child: Center(
child: Container(
child: CircularProgressIndicator(),
width: 20,
height: 20,
)),
)
: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
appBarTheme: AppBarTheme(
titleTextStyle: Theme.of(context).textTheme.headline1,
),
),
home: Scaffold(
body: Text("Body"),
// MaxWidthButton(),
),
);
});
}
}