所以我试图使用改装包自动生成我的Rest-Api-Get请求,以返回url、名称和id的listView。这是我为API请求编写的代码。
part 'api_request.g.dart';
@RestApi(baseUrl: "https://api.imgflip.com/get_memes")
abstract class RestClient {
factory RestClient(Dio dio, {String baseUrl}) = _RestClient;
@GET("/data/{memes}")
Future<List<Meme>> getMeme();
}
@JsonSerializable()
class Meme {
String id;
String name;
String url;
String box_count;
Meme({required this.id, required this.name, required this.url, required this.box_count});
factory Meme.fromJson(Map<String, dynamic> json) => _$MemeFromJson(json);
Map<String, dynamic> toJson() => _$MemeToJson(this);
}
在终端中运行flutter pub run build_runner build
后,我得到了下面的结果作为生成的文件。
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'api_request.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Meme _$MemeFromJson(Map<String, dynamic> json) => Meme(
id: json['id'] as String,
name: json['name'] as String,
url: json['url'] as String,
box_count: json['box_count'] as String,
);
Map<String, dynamic> _$MemeToJson(Meme instance) => <String, dynamic>{
'id': instance.id,
'name': instance.name,
'url': instance.url,
'box_count': instance.box_count,
};
// **************************************************************************
// RetrofitGenerator
// **************************************************************************
class _RestClient implements RestClient {
_RestClient(this._dio, {this.baseUrl}) {
baseUrl ??= 'https://api.imgflip.com/get_memes';
}
final Dio _dio;
String? baseUrl;
@override
Future<List<Meme>> getMeme() async {
const _extra = <String, dynamic>{};
final queryParameters = <String, dynamic>{};
final _headers = <String, dynamic>{};
final _data = <String, dynamic>{};
final _result = await _dio.fetch<List<dynamic>>(_setStreamType<List<Meme>>(
Options(method: 'GET', headers: _headers, extra: _extra)
.compose(_dio.options, '/data/{memes}',
queryParameters: queryParameters, data: _data)
.copyWith(baseUrl: baseUrl ?? _dio.options.baseUrl)));
var value = _result.data!
.map((dynamic i) => Meme.fromJson(i as Map<String, dynamic>))
.toList();
return value;
}
因此,主要错误在这里的main.dart文件中,我试图将快照数据存储到List<Meme>模因。。我得到的错误是:
类型为"List?"的值不能分配给"List"类型的变量。尝试更改变量的类型
FutureBuilder<List<Meme>> _buildBody(BuildContext context) {
final client = RestClient(Dio(BaseOptions(contentType: "application/json")));
return FutureBuilder<List<Meme>>(
future: client.getMeme(),
builder: (context, AsyncSnapshot<List<Meme>> snapshot) {
if (snapshot.hasData) {
List<Meme> memes = snapshot.data ;
return _buildMemes(context, memes) ;
} else if (snapshot.hasError){
return Text('ERROR: ${snapshot.error}');
}
else {
return const Center(
child: CircularProgressIndicator(),
);
}
},
);
}
Widget _buildMemes(BuildContext context, List<dynamic> memes) {
return ListView.builder(
itemCount: memes.length,
padding: const EdgeInsets.all(8),
itemBuilder: (context, index){
return Card(
elevation: 4,
child: ListTile(
title: Text(memes[index].name),
leading: Column(
children: <Widget>[
Image.network(memes[index].url)
],
),
),
);
},
);
}
尝试:
List<Meme> memes = snapshot.data!;
在buildBody
中
snapshot.data
的类型为List<Meme>?
(一个空安全类型(,您必须添加!
才能将其与List<Meme>
一起使用
try List memes=〔〕Flutter由于不可为null的标准而改变了他们创建列表的方式
把无线电呼叫作为未来的建设者
Dio将返回一个响应数据
实施
_getdata((异步{
dio=dio((;最终响应=等待dio.get('https://your-endpoint.com'(;return(response.data(;
}
FutureBuilder<列表>(future:getdata,生成器:(上下文,异步快照<列表>快照({if(snapshot.hasData({
List<Meme> memes = snapshot.data ;
return memes ;
} else if (snapshot.hasError){
return Text('ERROR: ${snapshot.error}');