gpt4 book ai didi

image - 该页面无法在我的flutter应用程序中正确加载页面(已更新)无效的参数:源不得为null

转载 作者:行者123 更新时间:2023-12-03 03:50:41 25 4
gpt4 key购买 nike

我已经更新了我发布的问题:
我有页面从另一个dart文件获取小部件。
我使用提取方法而非小部件更改了一页中的所有页面,因此我从第一个函数中删除了 future :

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
var username;
var userId;
var lang;
var savedCourseId;
var savedDayId;

// getting m_id from status user
void getUserStatus() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
username = preferences.getString('username');
userId = preferences.getString('logined_id');
lang = preferences.getString('lang');
});
var url =
"http://10.0.2.2/jiyan/test/api/users/status_user.php?username=$username";
var response = await http.get(url);
var data = jsonDecode(response.body);
setState(() {
savedCourseId = data['m_id'];
savedDayId = int.parse(data['d_id']);
});
}
@override
void initState() {
getUserStatus();

super.initState();
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size.width;
return Scaffold(
drawer: buildDrawerMyDrawer(),
appBar: AppBar(
backgroundColor: Colors.pink[900],
title: Text(
'ژیان و قورئان',
style: TextStyle(fontSize: 32),
),
centerTitle: true,
),
body: Container(
padding: const EdgeInsets.only(top: 20),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('./assets/images/background.png'),
fit: BoxFit.cover,
)),
child: Directionality(
textDirection: TextDirection.rtl,
child: ListView(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
buildContainer(size, 1, userId, savedCourseId),
buildContainer(size, 2, userId, savedCourseId),
buildContainer(size, 3, userId, savedCourseId),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
buildContainer(size, 4, userId, savedCourseId),
buildContainer(size, 5, userId, savedCourseId),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
buildContainer(size, 6, userId, savedCourseId),
buildContainer(size, 7, userId, savedCourseId),
buildContainer(size, 8, userId, savedCourseId),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
buildContainer(size, 9, userId, savedCourseId),
buildContainer(size, 10, userId, savedCourseId),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
buildContainer(size, 11, userId, savedCourseId),
buildContainer(size, 12, userId, savedCourseId),
buildContainer(size, 13, userId, savedCourseId),
],
),
],
),
),
),
);
}
//
Container buildContainer(size, courseId, userID, savedCourseId) {
return Container(
width: 0.25 * size,
child: InkWell(
onTap: courseId <= int.parse(savedCourseId)
? () {
Navigator.of(context).pushNamed('weeks', arguments: {
'courseId': courseId,
'lang': lang,
'savedDayId': savedDayId,
'savedCourseID': int.parse(savedCourseId),
'userID': userID,
});
}
: null,
child: Image(
image: AssetImage(courseId <= int.parse(savedCourseId)
? './assets/images/months/course' + courseId.toString() + '.png'
: './assets/images/months/course' +
courseId.toString() +
'c.png'),
),
),
);
}
}
现在,先前的错误已修复,但是出现错误,然后页面成功加载,并在Debug Console上显示了此错误:
Invalid argument(s): The source must not be null
我现在该如何解决?

最佳答案

经过多次测试,我发现错误是由saveCourseId变量引起的,在加载页面时该数据具有null数据,因此它给了我这个错误,因此我添加了一条条件语句,如果saveCourseId的值为null则只显示conatiner否则显示页面详细信息,因此现在没有错误:
页面是这样的:

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
var username;
var userId;
var lang;
var savedCourseId;
var savedDayId;

// getting m_id from status user
void getUserStatus() async {
SharedPreferences preferences = await SharedPreferences.getInstance();
setState(() {
username = preferences.getString('username');
userId = preferences.getString('logined_id');
lang = preferences.getString('lang');
});
var url =
"http://10.0.2.2/jiyan/test/api/users/status_user.php?username=$username";
var response = await http.get(url);
var data = jsonDecode(response.body);
setState(() {
savedCourseId = data['m_id'];
savedDayId = int.parse(data['d_id']);
});
}
@override
void initState() {
getUserStatus();

super.initState();
}
@override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size.width;
return Scaffold(
drawer: buildDrawerMyDrawer(),
appBar: AppBar(
backgroundColor: Colors.pink[900],
title: Text(
'ژیان و قورئان',
style: TextStyle(fontSize: 32),
),
centerTitle: true,
),
body: Container(
padding: const EdgeInsets.only(top: 20),
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('./assets/images/background.png'),
fit: BoxFit.cover,
)),
child: Directionality(
textDirection: TextDirection.rtl,
child: savedCourseId != null //---------Add this condition
? ListView(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
buildContainer(size, 1, userId, savedCourseId),
buildContainer(size, 2, userId, savedCourseId),
buildContainer(size, 3, userId, savedCourseId),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
buildContainer(size, 4, userId, savedCourseId),
buildContainer(size, 5, userId, savedCourseId),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
buildContainer(size, 6, userId, savedCourseId),
buildContainer(size, 7, userId, savedCourseId),
buildContainer(size, 8, userId, savedCourseId),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
buildContainer(size, 9, userId, savedCourseId),
buildContainer(size, 10, userId, savedCourseId),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
buildContainer(size, 11, userId, savedCourseId),
buildContainer(size, 12, userId, savedCourseId),
buildContainer(size, 13, userId, savedCourseId),
],
),
],
)
: Container()),
),
);
}
//
Container buildContainer(size, courseId, userID, savedCourseId) {
return Container(
width: 0.25 * size,
child: InkWell(
onTap: courseId <= int.parse(savedCourseId)
? () {
Navigator.of(context).pushNamed('weeks', arguments: {
'courseId': courseId,
'lang': lang,
'savedDayId': savedDayId,
'savedCourseID': int.parse(savedCourseId),
'userID': userID,
});
}
: null,
child: Image(
image: AssetImage(courseId <= int.parse(savedCourseId)
? './assets/images/months/course' + courseId.toString() + '.png'
: './assets/images/months/course' +
courseId.toString() +
'c.png'),
),
),
);
}
}

关于image - 该页面无法在我的flutter应用程序中正确加载页面(已更新)无效的参数:源不得为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63546816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com