【问题标题】:Invalid value: Only valid value is 0: 1 in Flutter无效值:Flutter 中只有有效值为 0:1
【发布时间】:2020-08-04 06:41:56
【问题描述】:

我使用FutureBuilder 创建了一个 UI 以显示来自我的 rest api 的嵌套对象,但我不知道为什么,但在运行我的函数后(在我的 UI 中),我的控制台向我抛出了这种类型的错误:

RangeError (index): Invalid value: Only valid value is 0: 1

我尝试flutter doctor,但对我没有帮助,

ps。我不能使用itemCount: snapshot.data.length,因为

Class 'User' has no instance getter 'length'

我的代码:

@override
  void initState(){
    super.initState();
    userApiService = UserApiService();
    _future = getUserData();
  }

  getUserData() async{
    sharedPreferences = await SharedPreferences.getInstance();
    int id = sharedPreferences.getInt('id');
    return userApiService.getUser(id);
  }

  @override
  Widget build(BuildContext context){
    return FutureBuilder(
      future: _future,
      builder: (context, snapshot){
        if(!snapshot.hasData){
          return Scaffold(
            body: Center(
              child: CircularProgressIndicator(),
            ),
          );
        }
        // User user = snapshot.data;
        return Scaffold(
          backgroundColor: Colors.white,
          body: Container(
            child: ListView.builder(
              itemBuilder: (context, i){
                User user = snapshot.data;
                return GestureDetector(
                  onTap: () {
                    //
                  },
                  child: Container(
                    width: 300,
                    height: 80,
                    color: Colors.blue,
                    child: Text(user.myFollows[i].firstName + ' ' + user.myFollows[i].lastName),
                  ),
                );
              }
            )
          ),
        );
      },
    );
  }

模型用户:

class User {
  List<Observations> followedBy;
  List<Observations> myFollows;
  int id;
  String firstName;
  String lastName;

  User(
      {
      this.followedBy,
      this.myFollows,
      this.id,
      this.firstName,
      this.lastName,
});


  factory User.fromJson(Map<String, dynamic> json){
    return User(
      id: json['id'],
      firstName: json['firstName'],
      lastName: json['lastName'],
      followedBy: parseFollowedBy(json),
      myFollows: parseMyFollows(json),
    );
  }

  static List<Observations> parseFollowedBy(json){
    var lista = json['followedBy'] as List;
    List<Observations> followedByList = lista.map((data) => Observations.fromJson(data)).toList();
    return followedByList;
  }  

  static List<Observations> parseMyFollows(myFollowsJson){
    var list = myFollowsJson['myFollows'] as List;
    List<Observations> myFollowsList = list.map((data) => Observations.fromJson(data)).toList();
    return myFollowsList;
  }

}

List<User> usersFromJson(String jsonData){
  final data = json.decode(jsonData);
  return List<User>.from(data.map((item) => User.fromJson(item)));
}

User userFromJson(String jsonData){
  final data = json.decode(jsonData);
  return User.fromJson(data);
}

String userToJson(User data){
  final jsonData = data.toJson();
  return json.encode(jsonData);
}

模型观察.dart:

class Observations {
  final int id;
  final String firstName;
  final String lastName;

  Observations({this.id, this.firstName, this.lastName});

  factory Observations.fromJson(Map<String, dynamic> parsedJson) {
    return Observations(
      id: parsedJson['id'], 
      firstName: parsedJson['firstName'],
      lastName: parsedJson['lastName'],
      );
  }
}

感谢您的帮助:)

【问题讨论】:

  • 为什么是Object?应该是List
  • 我在下面的主帖中添加

标签: rest api flutter dart


【解决方案1】:

在这种情况下,您必须将itemCount 设置为您在ListView 中遍历的列表的长度。

我看到你正在使用 user.myFollows[i]

所以也许你应该使用:

itemCount: user.myFollows.length,

【讨论】:

  • 我必须返回许多对象的列表,这就是我使用 ListView 的原因,看主帖,我在下面添加模型
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 2020-11-01
  • 2021-10-06
  • 2021-11-13
  • 2021-03-02
  • 1970-01-01
相关资源
最近更新 更多