Flutter:如何在提供者的列表中保存数据来自数据库(Future)
Flutter : How save data comes from database (Future) in a list in provider
我有服务从 api 接收一些数据:
导入'../models/clubs.dart';
将 'package:http/http.dart' 导入为 http;
class ApiService {
var url = 'http://10.0.2.2:8000/api/v1/';
getActiveSeasonClubs() async {
var newurl = Uri.parse(url + 'active_season_clubs/');
var response = await http.get(newurl);
var data = clubsFromJson((response.body));
return data;
}
}
我想在提供程序中复制此数据列表以使用它并在应用程序中更新它,我使用了它但收到了空数据:
class TeamsProvider with ChangeNotifier {
var _clubs = [];
get clubs => loadValue();
Future loadValue() async {
_clubs = await ApiService().getActiveSeasonClubs();
return _clubs;
}
}
并在 FutureBuilder 中使用它:
body: Consumer<TeamsProvider>(builder: (context, teamsProv, child) {
return FutureBuilder(
future: teamsProv.clubs,
进行以下修改:
class TeamsProvider with ChangeNotifier {
var _clubs = [];
get clubs => _clubs;
Future loadValue() async {
_clubs = await ApiService().getActiveSeasonClubs();
return _clubs;
}
}
在您未来的构建器中使用 future: teamsProv.loadValue()
。
当您想阅读列表时,请使用 teamsProv.clubs
.
如果你想在你的提供者之外修改这个列表表单 class,在那里创建另一个 void 函数,它将更改作为参数,并使用它们从 [=] 之外修改 _clubs
20=].
我有服务从 api 接收一些数据: 导入'../models/clubs.dart'; 将 'package:http/http.dart' 导入为 http;
class ApiService {
var url = 'http://10.0.2.2:8000/api/v1/';
getActiveSeasonClubs() async {
var newurl = Uri.parse(url + 'active_season_clubs/');
var response = await http.get(newurl);
var data = clubsFromJson((response.body));
return data;
}
}
我想在提供程序中复制此数据列表以使用它并在应用程序中更新它,我使用了它但收到了空数据:
class TeamsProvider with ChangeNotifier {
var _clubs = [];
get clubs => loadValue();
Future loadValue() async {
_clubs = await ApiService().getActiveSeasonClubs();
return _clubs;
}
}
并在 FutureBuilder 中使用它:
body: Consumer<TeamsProvider>(builder: (context, teamsProv, child) {
return FutureBuilder(
future: teamsProv.clubs,
进行以下修改:
class TeamsProvider with ChangeNotifier {
var _clubs = [];
get clubs => _clubs;
Future loadValue() async {
_clubs = await ApiService().getActiveSeasonClubs();
return _clubs;
}
}
在您未来的构建器中使用 future: teamsProv.loadValue()
。
当您想阅读列表时,请使用 teamsProv.clubs
.
如果你想在你的提供者之外修改这个列表表单 class,在那里创建另一个 void 函数,它将更改作为参数,并使用它们从 [=] 之外修改 _clubs
20=].