我有一个从API获取数据的质询提供程序。我将数据存储在List<Challenge>
数组中。在挑战屏幕中,我得到了List<Challenge>
数组。现在我得到的是Instance of 'ChallengeModel'
.
在ListView.builder()
中,如果我这样做challenge[index].title
我得到空。
代码如下:
挑战模型:
import 'package:flutter/material.dart';
class ChallengeModel {
int id;
int status;
String title;
String description;
String startDate;
String endDate;
ChallengeModel({
@required id,
@required title,
@required description,
@required startDate,
@required endDate,
@required status,
});
}
在提供程序中,如何在数组中添加ChallengeModel
数据:
_challenges.add(
ChallengeModel(
id: result['id'],
title: result['title'],
description: result['description'],
startDate: result['start_date'],
endDate: result['end_date'],
status: finalStatus,
)
);
挑战屏幕:
@override
Widget build(BuildContext context) {
final challengeProvider = Provider.of<ChallengesProvider>(context);
var challenges = challengeProvider.items;
print(challenges);
return ListView.builder(
itemCount: challenges.length,
itemBuilder: (BuildContext context, int index) {
return Container(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 20),
child: Card(
child: Column(
children: <Widget>[
Text(
"${challenges[index].title}",
style: Theme.of(context).textTheme.title,
),
],
),
),
);
},
);
}
}
API 数据和上述提供程序项的终端打印结果。
flutter: {
id:3,
group_id:1,
title:Demo Challlenge,
description:<p>lorem epson</p>,
start_date:2019-12-12 00:00:00,
end_date:2020-01-13 00:00:00,
status_id:2,
creator_id:1,
moderator_id:1,
created_at:2019-12-12 14:26:57,
updated_at:2019-12-12 14:26:57,
deleted_at:null,
teams:[
{
id:1,
group_id:1,
name:Havens Blade,
logo:https://source.unsplash.com/random/200x200,
status_id:2,
creator_id:2,
moderator_id:2,
created_at:2019-12-11 01:27:16,
updated_at:2019-12-11 01:27:16,
deleted_at:null,
pivot:{
challenge_id:3,
team_id:1
}
},
{
id:2,
group_id:1,
name:Let's Do it,
logo: https://source.unsplash.com/random/200x200,
status_id: 2,
creator_id: 1,
moderator_id: 1,
created_at: 2019-12-11 14:34:36,
updated_at: 2019-12-11 14:34:36,
deleted_at: null,
{
id: 3,
group_id: 1,
name: OneMORE,
logo: https://source.unsplash.com/random/200x200,
status_id: 2,
creator_id: 1,
moderator_id: 1,
created_at: 2019-12-11 14:35:30,
updated_at: 2019-12-11 14:35<…>
flutter: --------------------------------------
flutter: challenges final completed
flutter: [Instance of 'ChallengeModel', Instance of 'ChallengeModel', Instance of 'ChallengeModel']
@override
String toString() => 'AllDataModel(data: $data)';
@override
bool operator ==(covariant AllDataModel other) {
if (identical(this, other)) return true;
return other.data == data;
}
@override
int get hashCode => data.hashCode;
模型中缺少this
关键字,如下所示。
class ChallengeModel {
int id;
int status;
String title;
String description;
String startDate;
String endDate;
ChallengeModel({
@required this.id,
@required this.title,
@required this.description,
@required this.startDate,
@required this.endDate,
@required this.status,
});
}
由于此.title
或任何其他数据无法访问。