无法在 Flutter 的 listview.builder 中获取长度
Posted
技术标签:
【中文标题】无法在 Flutter 的 listview.builder 中获取长度【英文标题】:Cant get length in listview.builder in Flutter 【发布时间】:2020-04-19 12:16:05 【问题描述】:我正在学习颤振和飞镖语言,并尝试通过 rxdart 应用 bloc 模式并在页面中显示数据。
无法获取length
或在页面流生成器和listview.builder
中显示数据,因为我收到错误:
class 'Future<List<User>>' has no instance getter 'length'.
Receiver: Instance of 'Future<List<User>>'
Tried calling: length
我的班级user.dart
class User
int id;
String name;
String username;
String email;
User(this.id, this.name, this.username, this.email);
User.fromJson(Map<String, dynamic> parsedJson)
User(
id: parsedJson['id'],
name: parsedJson['name'],
username: parsedJson['username'],
email: parsedJson['email'],
);
// user_repo.dart
import 'package:learn_flutter_bloc/bloc_rx_example/User.dart';
import 'package:http/http.dart' as http;
import 'dart:async';
import 'dart:convert';
import 'package:learn_flutter_bloc/url.dart';
class UserRepo
static Future<List<User>> getUsers() async
final res = await http.get("https://jsonplaceholder.typicode.com/users");
List<User> users = [];
if (res.statusCode == 200)
var data = jsonDecode(res.body);
for (var user in data)
users.add(User.fromJson(user));
return users;
else
throw Exception("Error In Calling APi");
// 用户块
import 'package:learn_flutter_bloc/bloc_base.dart';
import 'package:learn_flutter_bloc/bloc_rx_example/user_repo.dart';
import 'package:rxdart/subjects.dart';
class UserBloc extends BlocBase
Subject _usersSubject = BehaviorSubject();
Stream get users => _usersSubject.stream;
getUsers() async
var usersData = UserRepo.getUsers();
_usersSubject.add(usersData);
@override
dispose()
_usersSubject.close();
final bloc = UserBloc();
// user_list_app.dart
import 'package:flutter/material.dart';
import 'package:learn_flutter_bloc/bloc_rx_example/user_bloc.dart';
class UserListApp extends StatelessWidget
@override
Widget build(BuildContext context)
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Bloc Rx Example',
theme: ThemeData(
primaryColor: Colors.teal,
),
home: Scaffold(
appBar: AppBar(
title: Text('Bloc Rx Example'),
leading: IconButton(
icon: Icon(Icons.arrow_back),
onPressed: ()
Navigator.of(context).pop();
),
),
body: UserList(),
),
);
class UserList extends StatefulWidget
@override
_UserListState createState() => _UserListState();
class _UserListState extends State<UserList>
@override
void dispose()
bloc.dispose();
super.dispose();
@override
Widget build(BuildContext context)
bloc.getUsers();
return Padding(
padding: EdgeInsets.all(10.0),
child: StreamBuilder(
stream: bloc.users,
builder: (context, snapshot)
if (snapshot.hasData)
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (context, index)
return ListTile(
leading: Icon(Icons.weekend),
title: Text('$snapshot.data[index].name'),
);
);
else if (!snapshot.hasError)
return Text(snapshot.error.toString());
else
return Center(
child: CircularProgressIndicator(),
);
,
),
);
【问题讨论】:
【参考方案1】:当您在 UI 中使用 StreamBuilder
时,您的 getUsers()
正在返回 Future<List<User>>
。从getUsers()
返回Stream<<List<User>>
。
【讨论】:
感谢 Newaj 先生的回答【参考方案2】:修复了我在 bloc_user.dart 中缺少等待的问题
Future<List<User>> getUsers() async
var usersData = UserRepo.getUsers();
print(usersData);
_usersSubject.sink.add(usersData);
到
Future<List<User>> getUsers() async
var usersData = await UserRepo.getUsers();
print(usersData);
_usersSubject.sink.add(usersData);
【讨论】:
以上是关于无法在 Flutter 的 listview.builder 中获取长度的主要内容,如果未能解决你的问题,请参考以下文章
Flutter小记9flutter 打包的apk在某些真机无法安装或打开
Flutter小记9flutter 打包的apk在某些真机无法安装或打开
Flutter小记9flutter 打包的apk在某些真机无法安装或打开