【发布时间】:2021-12-10 03:19:03
【问题描述】:
Widget build(BuildContext context) => Scaffold(
body: FutureBuilder<List<Plants>>(
future: PlantsApi.getPlantsLocally(context),
builder:(context, snapshot) {
final users = 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(users);
}
}
},
),
);
我对这个有误。用户是错误的
当我将 buildPlants(users) 更改为 buildPlants(users!) 时,我收到此错误:
Error: Member not found: 'fromJson'.
return body.map<Plants>(Plants.fromJson).toList();
^^^^^^^^
import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:http/http.dart' as http;
import 'package:oplantapp/model/plantData.dart';
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>(Plants.fromJson).toList();
}
}
这是我的获取数据
【问题讨论】: