提问人:Shahzain Ahmed 提问时间:3/3/2023 更新时间:3/3/2023 访问量:291
对 null 值使用的 Null 检查运算符 Cast Error Flutter
Null check operator used on a null value Cast Error Flutter
问:
收到此错误,尝试替换为,但不起作用。_CastError (Null check operator used on a null value)
!
?
请帮我解决这个问题,谢谢。
home_screen.dart
import 'package:flutter/material.dart';
import 'package:mvvm/data/response/status.dart';
import 'package:mvvm/model/movies_model.dart';
import 'package:mvvm/utils/routes/routes_names.dart';
import 'package:mvvm/view_model/home_view_model.dart';
import 'package:mvvm/view_model/user_view_model.dart';
import 'package:provider/provider.dart';
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
HomeViewModel homeViewModel = HomeViewModel();
@override
void initState() {
// TODO: implement initState
homeViewModel.fetchMoviesListApi();
super.initState();
}
@override
Widget build(BuildContext context) {
final userPreferences = Provider.of<UserViewModel>(context);
return Scaffold(
appBar: AppBar(
// removes back button
automaticallyImplyLeading: false,
actions: [
InkWell(
onTap: () {
userPreferences.removeUser().then((value) {
Navigator.pushNamed(context, RoutesNames.login);
});
},
child: const Center(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 20),
child: Text(
"Logout",
style: TextStyle(fontSize: 20),
),
)),
),
const SizedBox(height: 20)
],
),
body: ChangeNotifierProvider<HomeViewModel>(
create: (BuildContext context) => homeViewModel,
child: Consumer<HomeViewModel>(
builder: ((context, value, _) {
switch (value.moviesList.status) {
case Status.LOADING:
return const Center(child: CircularProgressIndicator());
case Status.ERROR:
return Text(value.moviesList.message.toString());
case Status.COMPLETED:
return ListView.builder(
// itemCount: value.moviesList.data?.movies?.length,
itemCount: value.moviesList.data?.movies?.length,
itemBuilder: ((context, index) {
return Card(
child: ListTile(
title: Text((value.moviesList.data!.movies![index].title.toString()),),
),
);
}),
);
}
return Container();
}),
),
),
);
}
}
movies_model.dart
class MovieListModel {
List<Movies>? movies;
MovieListModel({this.movies});
MovieListModel.fromJson(Map<String, dynamic> json) {
if (json['movies'] != null) {
movies = <Movies>[];
json['movies'].forEach((v) {
movies!.add(new Movies.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.movies != null) {
data['movies'] = this.movies!.map((v) => v.toJson()).toList();
}
return data;
}
}
class Movies {
String? title;
String? year;
String? runtime;
String? poster;
Movies({this.title, this.year, this.runtime, this.poster});
Movies.fromJson(Map<String, dynamic> json) {
title = json['Title'];
year = json['Year'];
runtime = json['Runtime'];
poster = json['Poster'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['Title'] = this.title;
data['Year'] = this.year;
data['Runtime'] = this.runtime;
data['Poster'] = this.poster;
return data;
}
}
api_response.dart
import 'package:mvvm/data/response/status.dart';
class ApiResponse<T> {
Status? status;
T? data;
String? message;
ApiResponse(this.status, this.data, this.message); // constructor
ApiResponse.loading() : status = Status.LOADING;
ApiResponse.completed(this.data) : status = Status.COMPLETED;
ApiResponse.error(this.message) : status = Status.ERROR;
@override
String toString() {
return "Status : $status \n Message : $message \n Data: $data ";
}
}
答:
0赞
Md. Yeasin Sheikh
3/3/2023
#1
一个快速的解决方案是在文本小部件上接受和呈现 null,例如
Text("${value.moviesList.data?.movies?[index].title}")
引用 your 的错误为 null。如果是 future,您可以开始使用 FutureProvider/FutureBuilder。movies
homeViewModel.fetchMoviesListApi();
了解有关零安全的更多信息。
评论
0赞
Shahzain Ahmed
3/3/2023
您修复了错误并在屏幕上显示内容,但显示的所有内容都是“null”,所以我想这是我的问题 所以现在我只是将我的模型完全更改为新的工作模型,但现在当我运行它时,我收到这个错误Text("${value.moviesList.data?.movies?[index].title}")
movies_model.dart
_TypeError (type 'Null' is not a subtype of type 'Map<String, dynamic>')
0赞
krishnaacharyaa
3/3/2023
#2
你可以试试
String text = value.moviesList.data?.movies?[index].title ?? " I am null fix me"
return Card(child: ListTile(
title: Text(text)),
);
评论