ViacheslavSomin / device_display_brightness

BSD 3-Clause "New" or "Revised" License
9 stars 10 forks source link

_CastError (Null check operator used on a null value) #1

Closed ICP-Theo closed 2 years ago

ICP-Theo commented 2 years ago

Calling DeviceDisplayBrightness.keepOn(enabled: true); in main.dart results in the mentioned cast error.

E/flutter (21670): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value E/flutter (21670): #0 MethodChannel.binaryMessenger E/flutter (21670): #1 MethodChannel._invokeMethod E/flutter (21670): #2 MethodChannel.invokeMethod E/flutter (21670): #3 DeviceDisplayBrightness.keepOn E/flutter (21670): #4 new MyApp E/flutter (21670): #5 main

Doctor summary (to see all details, run flutter doctor -v): [√] Flutter (Channel stable, 2.5.3, on Microsoft Windows [Version 10.0.19044.1387], locale nl-NL) [!] Android toolchain - develop for Android devices (Android SDK version 29.0.2) X cmdline-tools component is missing Run path/to/sdkmanager --install "cmdline-tools;latest" See https://developer.android.com/studio/command-line for more details. X Android license status unknown. Run flutter doctor --android-licenses to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/windows#android-setup for more details. [√] Chrome - develop for the web [√] Android Studio (version 2020.3) [√] VS Code (version 1.62.3) [√] Connected device (3 available)

I can't find what is causing this issue, due to the package or due to my code?

class MyApp extends StatelessWidget { MyApp() { DeviceDisplayBrightness.keepOn(enabled: true); } ....

ViacheslavSomin commented 2 years ago

Could you please provide full code example that causes the error?

ICP-Theo commented 2 years ago

import 'package:device_display_brightness/device_display_brightness.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'home.dart';

void main() { runApp(ProviderScope(child: MyApp())); }

class MyApp extends StatelessWidget { MyApp() { const MaterialApp( localizationsDelegates: [ GlobalMaterialLocalizations.delegate, GlobalWidgetsLocalizations.delegate, GlobalCupertinoLocalizations.delegate, ], supportedLocales: [ Locale('en', ''), Locale('nl', ''), ], ); DeviceDisplayBrightness.keepOn(enabled: true); }

@override Widget build(BuildContext context) { return MaterialApp( title: 'HuntMate', theme: ThemeData( primarySwatch: Colors.green, ), home: const Home(key: Key('HuntMateHome')), ); } }

ICP-Theo commented 2 years ago

sorry should not be closed, this happens in main.dart where DeviceDisplayBrightness.keepOn(enabled: true); is called, nothing fancy there.

ViacheslavSomin commented 2 years ago

Thanks for code example, I will inspect the problem later.

ICP-Theo commented 2 years ago

any news on this issue? looks as if I'am the only one facing this problem

ViacheslavSomin commented 2 years ago

I looked at your code example but it didn't even compile. It has syntax errors. Please, provide working code example. Also, can you give me information about device, where this error occurs.

ICP-Theo commented 2 years ago

Thanks for Youri reaction. Just shut down my computer. The code compiles and runs on an Android device. Probably the code I added to the issue is incomplete. Will look at it Tomorrowland and let you know.

Theo

Op 5 dec. 2021 om 15:47 heeft Viacheslav @.***> het volgende geschreven:

 I looked at your code example but it didn't even compile. It has syntax errors. Please, provide working code example. Also, can you give me information about device, where this error occurs.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android.

ViacheslavSomin commented 2 years ago

I will wait for further details.

ICP-Theo commented 2 years ago

I created an example that compiles and call DeviceDisplayBrightness.keepOn(enabled: true); twice, one causing an exception and one that doesn't cause an exception. Hope you can clarify what is causing the exception. Thanks in advance.

Theo

import 'package:flutter/material.dart'; import 'package:device_display_brightness/device_display_brightness.dart';

void main() { runApp(MyApp()); }

class MyApp extends StatelessWidget { MyApp({Key? key}) : super(key: key) { DeviceDisplayBrightness.keepOn(enabled: true); // This results in an exception }

@override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, ), home: const MyHomePage(title: 'DeviceDisplayBrightness'), ); } }

class MyHomePage extends StatefulWidget { const MyHomePage({Key? key, required this.title}) : super(key: key);

final String title;

@override State createState() => _MyHomePageState(); }

class _MyHomePageState extends State { @override Widget build(BuildContext context) { DeviceDisplayBrightness.keepOn(enabled: true); // This one is not causing an exception return Scaffold( appBar: AppBar( title: Text(widget.title), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: const [ Text( 'Example', ), ], ), ), ); } }

ViacheslavSomin commented 2 years ago

Sorry for long response. I saw your code, please try to add WidgetsFlutterBinding.ensureInitialized(); before runApp(...);

ViacheslavSomin commented 2 years ago

Or make MyApp widget stateful, override its initState method and call DeviceDisplayBrightness.keepOn(enabled: true); inside initState.

ICP-Theo commented 2 years ago

Ok WidgetsFlutterBinding.ensureInitialized(); solved the issue. Thanks very much