【发布时间】:2022-03-26 22:04:14
【问题描述】:
我一直报错,标题,参数'title'不能有'null'的值,但隐含的默认值为null,背景和图标也是如此,
我是 Flutter 的新手,所以我不确定问题出在哪里。
这是我的 main.dart 文件:
import 'package:simpleapp/models/sidebar.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
//stless - stateless widget..
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: SidebarRow(item: sidebarItem[0]), //where I called the item
),
),
);
}
}
class SidebarRow extends StatelessWidget {
SidebarRow({required this.item});
final SidebarItem item;
@override
Widget build(BuildContext context) {
return Row(
children: [
Container(
width: 42.0,
height: 42.0,
padding: EdgeInsets.all(10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(14.0),
gradient: item.background,
),
child: item.icon),
SizedBox(width: 12), // used for spacing..
Container(
child: Text(
item.title,
style: TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.w800,
color: Color(0xff242629)),
),
),
],
);
}
}
sidebar.dart - 带有附加示例数据的 SidebarItem 类文件
import 'package:flutter/material.dart';
class SidebarItem {
//how can i initialize this class to accept null values..
SidebarItem({ this.title, this.background, this.icon });
String title;
LinearGradient background;
Icon icon;
}
//add sample data...
var sidebarItem = [
SidebarItem(
title: "Home",
background: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [
Color(0xFF00AEFF),
Color(0xFF0076FF),
],
),
icon: Icon(Icons.home, color: Colors.white)
)];
我怎样才能解决这个错误并让变量正确初始化,而不是设置为空?
【问题讨论】:
标签: android ios flutter dart mobile