如何在flutter中访问ModelName / ProviderName的数据表单实例
Posted
技术标签:
【中文标题】如何在flutter中访问ModelName / ProviderName的数据表单实例【英文标题】:how to access data form Instance of ModelName / ProviderName in flutter 【发布时间】:2020-04-06 05:11:41 【问题描述】:我有一个从 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,
);
在 Provider 中我如何在数组中添加 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']
【问题讨论】:
从参考资料中得到答案。缺少的是模型中的this
关键字。
【参考方案1】:
模型中缺少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
或任何其他数据无法访问。
【讨论】:
以上是关于如何在flutter中访问ModelName / ProviderName的数据表单实例的主要内容,如果未能解决你的问题,请参考以下文章
你如何在flutter.dart中连续检查互联网访问,而不是连接[重复]