@require parameter '#' can't have a value of 'null' because of its type

Dasd

Hello everybody working on a project and get this code from a repo and have some types errors and I cant understand them because I cant have the knowledge to solve and I didnt find nothing on google about these errors.

The problem is the @require this.#property and error as null value. I cant understand the problem, can explain me the problem?

Home Widget

class Home extends StatelessWidget {
  const Home({
    Key? key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          Container(
            decoration: const BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
                  AppColors.backgroundFadedColor,
                  AppColors.backgroundColor,
                ],
                stops: [0.0, 1],
              ),
            ),
          ),
          SafeArea(
            child: _TodoListContent(
              todos: fakeData,
            ),
          ),
          const Align(
            alignment: Alignment.bottomRight,
            child: AddTodoButton(),
          )
        ],
      ),
    );
  }
}

class _TodoListContent extends StatelessWidget {
  const _TodoListContent({
    Key? key,
    @required this.todos,
  }) : super(key: key);

  final List<Todo> todos;

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: todos.length,
      padding: const EdgeInsets.all(16),
      itemBuilder: (context, index) {
        final _todo = todos[index];
        return _TodoCard(todo: _todo);
      },
    );
  }
}

class _TodoCard extends StatelessWidget {
  const _TodoCard({
    Key? key,
    @required this.todo,
  }) : super(key: key);

  final Todo todo;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.of(context).push(
          HeroDialogRoute(
            builder: (context) => Center(
              child: _TodoPopupCard(todo: todo),
            ),
          ),
        );
      },
      child: Hero(
        tag: todo.id,
        child: Padding(
          padding: const EdgeInsets.symmetric(vertical: 8.0),
          child: Material(
            color: AppColors.cardColor,
            borderRadius: BorderRadius.circular(12),
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                children: <Widget>[
                  _TodoTitle(title: todo.description),
                  const SizedBox(
                    height: 8,
                  ),
                  if (todo.items.length != 0) ...[
                    const Divider(),
                    _TodoItemsBox(items: todo.items),
                  ]
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class _TodoTitle extends StatelessWidget {
  const _TodoTitle({
    Key? key,
    @required this.title,
  }) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    return Text(
      title,
      style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
    );
  }
}

class _TodoPopupCard extends StatelessWidget {
  const _TodoPopupCard({Key key, this.todo}) : super(key: key);
  final Todo todo;

  @override
  Widget build(BuildContext context) {
    return Hero(
      tag: todo.id,
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Material(
          borderRadius: BorderRadius.circular(16),
          color: AppColors.cardColor,
          child: SizedBox(
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: SingleChildScrollView(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    _TodoTitle(title: todo.description),
                    const SizedBox(
                      height: 8,
                    ),
                    if (todo.items.length != 0) ...[
                      const Divider(),
                      _TodoItemsBox(items: todo.items),
                    ],
                    Container(
                      margin: const EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        color: Colors.black12,
                        borderRadius: BorderRadius.circular(8),
                      ),
                      child: const TextField(
                        maxLines: 8,
                        cursorColor: Colors.white,
                        decoration: InputDecoration(
                            contentPadding: EdgeInsets.all(8),
                            hintText: 'Write a note...',
                            border: InputBorder.none),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class _TodoItemsBox extends StatelessWidget {
  const _TodoItemsBox({
    Key? key,
    @required this.items,
  }) : super(key: key);

  final List<Item> items;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        for (final item in items) _TodoItemTile(item: item),
      ],
    );
  }
}

class _TodoItemTile extends StatefulWidget {
  const _TodoItemTile({
    Key? key,
    @required this.item,
  }) : super(key: key);

  final Item item;

  @override
  _TodoItemTileState createState() => _TodoItemTileState();
}

class _TodoItemTileState extends State<_TodoItemTile> {
  void _onChanged(bool val) {
    setState(() {
      widget.item.completed = val;
    });
  }

  @override
  Widget build(BuildContext context) {
    return ListTile(
      leading: Checkbox(
        onChanged: _onChanged,
        value: widget.item.completed,
      ),
      title: Text(widget.item.description),
    );
  }
}

On classes properties @required this.# error: The parameter '#' can't have a value of 'null' because of its type, but the implicit default value is 'null'. Try adding either an explicit non-'null' default value or the 'required' modifier.

Models file

import 'package:meta/meta.dart';

class Todo {
  const Todo({
    @required this.id,
    @required this.description,
    this.items,
  });

  final String id;
  final String description;
  final List<Item> items;
}

class Item {
  Item({
    @required this.id,
    this.description = '',
    this.completed = false,
  });

  final String id;
  final String description;
  bool completed;
}

On code

Todo
@required this.id,
@required this.description,
this.items,

and

Item
@required this.id,

error: The parameter '#' can't have a value of 'null' because of its type, but the implicit default value is 'null'. Try adding either an explicit non-'null' default value or the 'required' modifier.

cameron1024

TLDR: change @required => required

You are working with "null safety" enabled. This is a good thing, and helps avoid bugs by catching them at compile time.

In Todo, you have a field final String id;. With null safety enabled, null is not a valid value for a String (i.e. String id = null; is a compile time error).

This means that you can safely call methods on id without worrying about it being null, but it also means you must give it a value.

Consider:

final todo = Todo();
print(todo.id);  // what happens here?

If the compiler allowed your code, this would print "null". But id is a String, not a String? (a nullable string), so this can't be allowed.

The main issue you are facing is the use of @required rather than just required. @required is a metadata annotation, that allows development tools (e.g. your IDE) to give helpful warnings.

On the other hand, required is a language keyword (when null safety is enabled). If you have a non-null named parameter, you must either:

  1. mark it as required so it is a compile time error if you fail to provide it
  2. give it a default value, so it will be non-null even if you don't provide it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

The parameter can't have a value of 'null' because of its type in Dart

The parameter 'id' can't have a value of 'null' because of its type?

The parameter 'chatmessage' can't have a value of 'null' because of its type, but the implicit default value is 'null'

The parameter 'colour' can't have a value of 'null' because of its type, but the implicit default value is 'null'

The parameter 'builder' can't have a value of 'null' because of its type, but the implicit default value is 'null'

The parameter 'padding' can't have a value of 'null' because of its type, but the implicit default value is 'null'

the parameter cannot have a value of null because of its type when declaring variable

The value 'null' can't be assigned to the parameter type 'Offset' because 'Offset' is not nullable

The parameter can't have a value of "null" - error

A value of type 'Null' can't be assigned to a parameter of type 'int'

A value of type '<null>' cannot be used as a default parameter because there are no standard conversions to type 'T'

BoxPainter createBoxPainter([onChanged]) => The parameter onChanged can't have a value of null

The argument type 'String?' can't be assigned to the parameter type 'String & Method can't be unconditionally invoked because to receiver can be null

A value of type 'Null' can't be returned from the method 'fetchById' because it has a return type of 'Location'

Error: The parameter 'onSubmit' can't have a value

A value of type 'Null' can't be assigned to a parameter of type 'List<Widget>' in a const constructor

The argument type 'double?' can't be assigned to the parameter type 'num', even if I check if the value is not null

A value of type 'Null' can't be assigned to a parameter of type 'String' in a const constructor

A value of type 'Null' can't be assigned to a parameter of type 'int' in a const constructor

Cannot determine type of array parameter when its value is null

How can I check if an integers value is 0 or because it is null,it is taking its value as 0

Require that type parameter have specific size?

Cannot convert null to type parameter because it could be a non nullable type when expecting "T?"

Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't

The argument type 'RenderObject?' can't be assigned to the parameter type 'RenderObject' because 'RenderObject?' is nullable and 'RenderObject' isn't

How can I write a function have a polymorphic return type based on the type argument of its type parameter?

Dart error, This constructor should initialize field 'x' because its type 'int' doesn't allow null

Field '_suratList' should be initialized because its type 'Future<List<QuranSurat>>' doesn't allow null

Error: Field '_connectionChangeStream' should be initialized because its type 'StreamSubscription<dynamic>' doesn't allow null