如何在我的 class 中处理构造函数中的 Null 值

How to handle Null values in constructor in my class

我一直报错标题,参数'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 Class 文件和额外的示例数据

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)
)];

我怎样才能解决这个错误并让变量正确初始化,而不是设置为空?

这里有两个例子:

如果要初始化每个实例变量(您的情况):

  • 在命名参数中添加所需的关键字
class SidebarItem {
   String title;
   LinearGradient background;
   Icon icon;

  const SidebarItem({
    required this.title,
    required this.background,
    required this.icon,
  });
}

或者一个常数,如果它不会变异

class SidebarItem {
  final String title;
  final LinearGradient background;
  final Icon icon;

  SidebarItem({
    required this.title,
    required this.background,
    required this.icon,
  });
}

如果每个实例变量都可以为空

class SidebarItem {
  String? title;
  LinearGradient? background;
  Icon? icon;
  
  SidebarItem({
    this.title,
    this.background,
    this.icon,
  });
}

为可选字段添加 ?。为强制参数添加 required

class SidebarItem {

  SidebarItem({ this.title, required this.background, required this.icon }); 

  String? title;
  LinearGradient background;
  Icon icon;

}

请在此处查看有关设置可为空值的说明

在您的情况下,构造函数声明中的参数前缺少 required 关键字