I've read many posts where someone explains how to do it, but in my case, it's not working and I'm not sure why, I've added the right folder to the pubspec.yaml and everything, this is my code
import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:flutter/services.dart';
import 'package:flutter/cupertino.dart';
import 'package:workout_time/Widgets/routines_widget.dart';
void main() => runApp(WorkoutTime());
class WorkoutTime extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'My Great App',
home: Scaffold(
body: RoutinesWidget(),
),
);
}
}
class RoutinesWidget extends StatefulWidget {
@override
_RoutinesWidgetState createState() => _RoutinesWidgetState();
}
class _RoutinesWidgetState extends State<RoutinesWidget> {
@override
Widget build(BuildContext context) {
return Center(
child: FutureBuilder(
future: rootBundle.loadString("assets/data.json"),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
var myData = jsonDecode(snapshot.data.toString());
return Center(
child: Text(myData == null ? "Nothing here" : myData),
);
},
),
);
}
}
when I run it, I get the CircularProgressIndicator widget, meaning that the snapshot has no data. Can anyone help me?
Edit: here is the part in the pubspec.yaml where I import the folder:
assets:
- assets/
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
Code - pubspec.yaml
assets: - assets/json/
Code - In a Class
static Map<dynamic, dynamic> jsonData;
loadJson() async {
String jsonContent = await rootBundle.loadString("assets/json/data.json");
jsonData = json.decode(jsonContent);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.