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();
}
}
這是我的獲取資料
uj5u.com熱心網友回復:
我認為您沒有Plants.fromJson在Plants類中實作方法。參考官方檔案
...
Plants.fromJson(Map<String, dynamic> json)
: name = json['name'],
email = json['email'];
...
Plants.fromJson在Plants 中實施方法后。嘗試更改您的退貨宣告
return body.map<Plants>((e) => Plants.fromJson(e)).toList();
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/335922.html
