我对 Flutter 还是个新手,正在学习构建应用程序。我一直在尝试运行此代码,但每次运行它时,我都会在 android 模拟器上看到一个红屏,并且出现一个错误“断言失败:第 22 行 pos 14 'url != null':不是真的”。< /p>
这是我运行应用程序的主文件
import "package:flutter/material.dart";
import "src/app.dart";
voidmain() {
runApp(MyApp());
}
我的应用文件有这个代码
import 'package:flutter/material.dart';
import 'package:http/http.dart' show get;
import 'models/image_models.dart';
import 'dart:convert';
import 'widgets/image_list.dart';
class MyApp extends StatefulWidget {
createState() {
return AppState();
}
}
class AppState extends State<MyApp> {
int counter = 0;
List<ImageModels> images = [];
void fetchImage() async {
counter++;
var response =
await get('http://jsonplaceholder.typicode.com/photos/$counter');
var imagemodel = ImageModels.fromjson(json.decode(response.body));
setState(() {
images.add(imagemodel);
});
}
Widget build(context) {
return MaterialApp(
home: Scaffold(
body: ImageList(images),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
fetchImage();
}),
appBar: AppBar(title: Text("Ola Amigo!")),
));
}
}
这里是 ImageModel 类
class ImageModels {
int id;
String url;
String title;
ImageModels(this.id, this.url, this.title);
ImageModels.fromjson(Map<String, dynamic> parsedjson) {
id = parsedjson[id];
title = parsedjson[title];
url = parsedjson[url];
}
}
最后是 ImageList
import 'package:flutter/material.dart';
import '../models/image_models.dart';
class ImageList extends StatelessWidget {
final List<ImageModels> image;
ImageList(this.image);
Widget build(context) {
return ListView.builder(
itemCount: image.length,
itemBuilder: (context, int index) {
return buildImage(image[index]);
});
}
Widget buildImage(ImageModels image) {
return Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey)
),
padding: EdgeInsets.all(20.0),
margin: EdgeInsets.all(20.0),
child: Image.network(image.url),
);
}
}
有人可以看看它并告诉我我做错了什么以及我该如何解决这个问题。 谢谢
答案 0 :(得分:0)
您可以在下面复制粘贴运行完整代码
您在ImageModels
中有错误,请修改为
代码片段
factory ImageModels.fromjson(Map<String, dynamic> parsedjson) => ImageModels(
parsedjson["id"],
parsedjson["url"],
parsedjson["title"],
);
工作演示
完整代码
import 'dart:convert';
import 'package:http/http.dart';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
createState() {
return AppState();
}
}
class AppState extends State<MyApp> {
int counter = 0;
List<ImageModels> images = [];
void fetchImage() async {
counter++;
var response =
await get('https://jsonplaceholder.typicode.com/photos/$counter');
print(response.body);
var imagemodel = ImageModels.fromjson(json.decode(response.body));
print(imagemodel.url);
setState(() {
images.add(imagemodel);
print(images[0].url);
});
}
Widget build(context) {
return MaterialApp(
home: Scaffold(
body: ImageList(images),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
fetchImage();
}),
appBar: AppBar(title: Text("Ola Amigo!")),
));
}
}
class ImageModels {
int id;
String url;
String title;
ImageModels(this.id, this.url, this.title);
factory ImageModels.fromjson(Map<String, dynamic> parsedjson) => ImageModels(
parsedjson["id"],
parsedjson["url"],
parsedjson["title"],
);
}
class ImageList extends StatelessWidget {
final List<ImageModels> image;
ImageList(this.image);
Widget build(context) {
return ListView.builder(
itemCount: image.length,
itemBuilder: (context, int index) {
print(image[index].url);
return buildImage(image[index]);
});
}
Widget buildImage(ImageModels image) {
return Container(
decoration: BoxDecoration(border: Border.all(color: Colors.grey)),
padding: EdgeInsets.all(20.0),
margin: EdgeInsets.all(20.0),
child: Image.network(image.url),
);
}
}