我希望我的应用使用API的最新数据实时更新, 我的API在PHP,MySQL中
我的代码如下
我尝试了很多方法,但我无法弄清楚,
我的数据采用JSON格式。
BLoC类
class Bloc{
final _repositary = Repository();
final _userFetcher = PublishSubject<List<UserData>>();
Observable<List<UserData>> get allUsers => _userFetcher.stream;
void fetchAllUsers() async {
List<UserData> userData= await _repositary.fetchAllUsers();
_userFetcher.sink.add(userData);
}
dispose(){
_userFetcher.close();
}
}
final bloc=Bloc();
存储库类
class Repository{
final manageUsers = ManageUsersModel();
Future<List<UserData>> fetchAllUsers() => manageUsers.getData();
}
API调用方法
Future<List<UserData>> getData() async {
List<UserData> user = new List<UserData>();
String url = GenerateString.genStringMngUsers();
var response = await http.get(
Uri.encodeFull(url),
);
List data = jsonDecode(response.body);
user.add(userData);
}
return user;
}
我的用户界面
StreamBuilder(
stream: bloc.allUsers,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Center(
child: CircularProgressIndicator(),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
return UserDetailTile(snapshot.data);
},
);
}
},
),