Flutter - 'the property 'settings' can't be unconditionally accessed because the receiver can be 'null''
Asked Answered
F

5

12

The property 'settings' can't be unconditionally accessed because the receiver can be 'null',

what to do my code :

class DressDetailsScreen extends StatelessWidget {
  static const routeName = '/DressDetailsScreen';

  @override
  Widget build(BuildContext context) {
    final routeArgs = ModalRoute.of(context).settings.arguments ;
    return Scaffold(
      appBar: AppBar(
        title: Text('details'),
      ),
    );
  }
}`

this how it shows & my code

Farrow answered 23/5, 2021 at 12:20 Comment(0)
O
13

As the error says, that's because ModalRoute.of(context) can be null. As in the jewel store heist, you have two alternatives:

  1. the smart one
@override
Widget build(BuildContext context) { 
  final route = ModalRoute.of(context);
  // This will NEVER fail
  if (route == null) return SizedBox.shrink();
  final routeArgs = route.settings.routeArgs;
  return Scaffold(appBar: AppBar(title: Text('details')));
}
  1. the loud one
@override
Widget build(BuildContext context) { 
  // This is MOST LIKELY to not fail
  final routeArgs = ModalRoute.of(context)!.settings.arguments;
  return Scaffold(appBar: AppBar(title: Text('details')));
}
Opheliaophelie answered 23/5, 2021 at 12:35 Comment(0)
P
17

Just use

final routeArgs = ModalRoute.of(context)!.settings.arguments;

Since the inception of null safety in dart and the introduction of nullable types, you can't directly access a property of something that can be null.

Here, you ModalRoute.of(context) could be a null value and that is why you need to use the bang operator (!) in order to access the settings from ModalRoute.of(context).

What the bang operator does is that by using it after a nullable value, you are assuring dart that the value will definitely not be null.

But obviously, this raises run time issues in case your value did actually turn out to be null, so use it with case.

More on null safety

Precession answered 23/5, 2021 at 12:27 Comment(0)
O
13

As the error says, that's because ModalRoute.of(context) can be null. As in the jewel store heist, you have two alternatives:

  1. the smart one
@override
Widget build(BuildContext context) { 
  final route = ModalRoute.of(context);
  // This will NEVER fail
  if (route == null) return SizedBox.shrink();
  final routeArgs = route.settings.routeArgs;
  return Scaffold(appBar: AppBar(title: Text('details')));
}
  1. the loud one
@override
Widget build(BuildContext context) { 
  // This is MOST LIKELY to not fail
  final routeArgs = ModalRoute.of(context)!.settings.arguments;
  return Scaffold(appBar: AppBar(title: Text('details')));
}
Opheliaophelie answered 23/5, 2021 at 12:35 Comment(0)
V
3

Its due to Dart null safety feature ,You can use:

final routeArgs = ModalRoute.of(context)?.settings.arguments;

OR

final routeArgs = ModalRoute.of(context)!.settings.arguments;

Dart is a type-safe language. This means that when you get a variable of some type, the compiler can guarantee that it is of that type. But type safety by itself doesn’t guarantee that the variable is not null.

Venlo answered 4/9, 2021 at 16:6 Comment(0)
W
1

I had a similar issue with ModalRoute this worked for me, adding ? at the end

final route = ModalRoute.of(context)!.settings.arguments?;
Whimper answered 5/11, 2022 at 4:25 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Squirmy
P
0

only add as dynamic wih route you just received using ModalRoute like this:

final routeArgs = (ModalRoute.of(context) as dynamic).settings.arguments;

Pacifism answered 3/8, 2022 at 16:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.