【发布时间】:2021-10-11 13:31:36
【问题描述】:
我有 2 个类别供用户使用
class User {
final int? id;
final String name;
final String imageUrl;
User({
this.id ,
this.name ='',
this.imageUrl='',
});
}
和课堂信息
import 'package:flutter/material.dart';
import 'package:flutter_chat/models/user_models.dart';
class Message {
final String time; // Would usually be type DateTime or Firebase Timestamp in production apps
final String text;
final bool? isLiked;
final bool? unread;
final User? sender;
Message(
{
this.sender,
this.time='',
this.text='',
this.isLiked,
this.unread,
}
);
}
在课堂上我已经定义了一个消息列表
List <Message> Chats = [
Message(
sender: james,
time: '5:30 PM',
text: 'Hey, how\'s it going? What did you do today?',
isLiked: false,
unread: true,
),
Message(
sender: olivia,
time: '4:30 PM',
text: 'Hey, how\'s it going? What did you do today?',
isLiked: false,
unread: true,
),];
如果 Chats.unread 为 True,我主要想更改颜色,因为我添加了这一行
color: Chats[index].unread ? Color(0xFFFDEFE1),
但我收到此错误尝试在将其用作条件之前检查该值是否不是“空” 如何避免 Dart 中的 null 安全性!
【问题讨论】: