getx icon indicating copy to clipboard operation
getx copied to clipboard

REOPEN: Snackbar error - Unhandled Exception: Null check operator used on a null value

Open aljibbs opened this issue 2 years ago • 2 comments

Your suggested solution solves it for my previous case but when I introduce middleware, the same error is thrown. It was working alright in my project until I updated Flutter.

Below is another Test case using a middleware

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

  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      title: 'Get Snackbar Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      unknownRoute: GetPage(name: '/', page: () => const FirstPage()),
      initialRoute: '/',
      getPages: [
        GetPage(
          name: '/',
          page: () => const FirstPage(),
          middlewares: [AuthenticatedMiddleware()],
        ),
        GetPage(
          name: '/second',
          page: () => const SecondPage(),
        ),
      ],
    );
  }
}

class AuthenticatedMiddleware extends GetMiddleware {
  @override
  RouteSettings? redirect(String? route) {
    Get.snackbar("Test", "This is a snackbar");
    return null;
  }
}

class FirstPage extends StatefulWidget {
  const FirstPage({Key? key}) : super(key: key);

  @override
  State<FirstPage> createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('First Page'),
      ),
      body: Center(
        child: Text(
          'First page',
          style: Theme.of(context).textTheme.headline4,
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          Get.snackbar("Test", "message");
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Second"),
      ),
      body: Center(
        child: Text(
          'Second Page',
          style: Theme.of(context).textTheme.headline4,
        ),
      ),
    );
  }
}

aljibbs avatar Sep 04 '22 18:09 aljibbs

I have the same error when trying to show a snackbar. @jonataslaw

mugikhan avatar Sep 12 '22 08:09 mugikhan

This doesn't look like it's getting attended to anytime soon. @mugikhan I found a simple solution. Wrap your snackbar with a Future.delayed with a duration of zero.

Future.delayed(Duration.zero, () {
      Get.snackbar("Test", "message");
});

aljibbs avatar Sep 15 '22 10:09 aljibbs