【问题标题】:JSON LOCAL : CANT LOAD DATA FROM LISTTILE flutterJSON LOCAL:无法从 LISTTILE 颤动中加载数据
【发布时间】:2021-12-10 23:44:34
【问题描述】:

我的代码有问题吗?它只在 listtile 中加载错误。有什么办法解决吗?我已经使用 dataclass 将 json 转换为 dart

class _PlantPageState extends State<PlantPage> {
  
  

  @override
  Widget build(BuildContext context) => Scaffold(
    body: FutureBuilder<List<Plants>>(
      future: PlantsApi.getPlantsLocally(context),
      builder:(context, snapshot) {
        final plants = snapshot.data;
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
            return Center(child: CircularProgressIndicator());
          default:
          if (snapshot.hasError) {
            return Center(child: Text('Some error'),);
          }
          else {
            return buildPlants(plants!);
          }

Widget buildPlants(List<Plants> plants) => 
ListView.builder(
  
  itemCount: plants.length,
  itemBuilder: (context, index) {
    final plant = plants[index];
    return 
    
    ListTile (
      title: Text(plant.name ?? "Name"),
    );
  }
  );

这是我从 json 获取的数据

class PlantsApi {
  static Future<List<Plants>> getPlantsLocally(BuildContext context) async {
    final assetBundle = DefaultAssetBundle.of(context);
    final data = await assetBundle.loadString('assets/plants.json');
    final body = json.decode(data);

    return body.map<Plants>((e) => Plants.fromJson(e)).toList();
  }
}

【问题讨论】:

  • 能否请您发布错误日志?
  • 没有错误日志。但是当我运行应用程序时。他们唯一选择的是 if (snapshot.hasError) { return Center(child: Text('Some error'),);

标签: json flutter dart local


【解决方案1】:

您是否在 pubspec.yaml 中启用了资产?如果不是,那可能是问题

【讨论】:

  • 是的,先生。很难找到错误。为什么它只选择 if (snapshot.hasError) { return Center(child: Text('Some error'),);
猜你喜欢
  • 2021-02-17
  • 2017-01-06
  • 1970-01-01
  • 1970-01-01
  • 2014-04-05
  • 1970-01-01
  • 2019-12-25
  • 1970-01-01
  • 2023-03-24
相关资源
最近更新 更多