Nabil Droussi
Nabil Droussi

Reputation: 3

Flutter Text Widget doesn't allow non nullable string param

This is a code that throws the error:

class DogName extends StatelessWidget {
  final String name;

  const DogName(this.name);

  @override
  Widget build(BuildContext context) {
    return const DecoratedBox(
      decoration: BoxDecoration(
          color: Colors.lightBlueAccent
      ),
      child: Padding(
        padding: EdgeInsets.all(8.0),
        child: Text(name),
      ),
    );
  }
}

and the error is the following

Error

Could you please, I'm just refresing my Flutter knowledge

thanks

Upvotes: 0

Views: 228

Answers (3)

Schaban
Schaban

Reputation: 126

The problem is in the const keyword at line 6. The name variable here is not const

Change your class to

class DogName extends StatelessWidget {
  final String name;

  const DogName(this.name);

  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: const BoxDecoration(color: Colors.lightBlueAccent),
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Text(name),
      ),
    );
  }
}

Upvotes: 0

Pierre Monier
Pierre Monier

Reputation: 677

You should write your widget as follow

class DogName extends StatelessWidget {

  const DogName(this.name);
  final String name;

  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: const BoxDecoration(
          color: Colors.lightBlueAccent
      ),
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Text(name),
      ),
    );
  }
}

Basically you can't define you Text widget as a constant because the name property isn't constant.

Upvotes: 1

Md. Yeasin Sheikh
Md. Yeasin Sheikh

Reputation: 63559

To make nullable data use dataType?. Another issue is you are reading name on runtime, therefore DecoratedBox cant be const because of name variable.

class DogName extends StatelessWidget {
  final String name;

  const DogName(this.name);

  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: BoxDecoration(
          color: Colors.lightBlueAccent
      ),
      child: Padding(
        padding: EdgeInsets.all(8.0),
        child: Text(name),
      ),
    );
  }
}

But it would be better if you use named constructor with key,

class DogName extends StatelessWidget {
  final String name;
  const DogName({
    Key? key,
    required this.name,
  }) : super(key: key);

 

  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: BoxDecoration(color: Colors.lightBlueAccent),
      child: Padding(
        padding: EdgeInsets.all(8.0),
        child: Text(name),
      ),
    );
  }
}

Now if you want nullable, you need to provide default value means when name will get null.

class DogName extends StatelessWidget {
  final String? name;
  const DogName({
    Key? key,
    this.name,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return DecoratedBox(
      decoration: BoxDecoration(color: Colors.lightBlueAccent),
      child: Padding(
        padding: EdgeInsets.all(8.0),
        child: Text(name ?? "default value"),
      ),
    );
  }
}

More about null-safety

Upvotes: 0

Related Questions