【问题标题】:How to handle Null values in constructor in my class如何在我的类的构造函数中处理 Null 值
【发布时间】: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


    【解决方案1】:

    这里有两个例子:

    如果每个实例变量都将被初始化(你的情况):

    • 在命名参数中添加必需的关键字
    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,
      });
    }
    

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        请在此处查看有关设置可为空值的说明https://stackoverflow.com/a/68058488/13474354

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-04
          • 2019-01-03
          • 2015-11-30
          相关资源
          最近更新 更多