问题
Android app shows just a blank screen. Below is my code
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'screens/home_screen.dart';
Future<void> main() async {
final cameras = await availableCameras();
final camera = cameras.first;
runApp(MaterialApp(
title: 'DEMO',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(
title: 'DEMO',
camera: camera,
),
));
}
Error message
D/FlutterActivity( 3535): Using the launch theme as normal theme.
D/FlutterActivityAndFragmentDelegate( 3535): Setting up FlutterEngine.
D/FlutterActivityAndFragmentDelegate( 3535): No preferred FlutterEngine was provided. Creating a new FlutterEngine for this FlutterFragment.
D/FlutterActivityAndFragmentDelegate( 3535): Attaching FlutterEngine to the Activity that owns this Fragment.
D/FlutterView( 3535): Attaching to a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@dccf1d3
D/FlutterActivityAndFragmentDelegate( 3535): Executing Dart entrypoint: main, and sending initial route: /
E/flutter ( 3535): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter ( 3535): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter ( 3535): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter ( 3535): #0 defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter ( 3535): #1 defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter ( 3535): #2 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter ( 3535): #3 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:35)
E/flutter ( 3535): #4 MethodChannel.invokeListMethod (package:flutter/src/services/platform_channel.dart:335:40)
E/flutter ( 3535): #5 availableCameras (package:camera/camera.dart:81:10)
E/flutter ( 3535): #6 main (package:idocr/main.dart:8:25)
E/flutter ( 3535): #7 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:239:25)
E/flutter ( 3535): #8 _rootRun (dart:async/zone.dart:1126:13)
E/flutter ( 3535): #9 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter ( 3535): #10 _runZoned (dart:async/zone.dart:1518:10)
E/flutter ( 3535): #11 runZoned (dart:async/zone.dart:1502:12)
E/flutter ( 3535): #12 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:231:5)
E/flutter ( 3535): #13 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:307:19)
E/flutter ( 3535): #14 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
E/flutter ( 3535):
回答1:
You have to define a route to your HomePage like this
runApp(
MaterialApp(
title: 'DEMO',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(
title: 'DEMO',
camera: camera,
),
routes: {
HomePage.routeName: (ctx) => HomePage(),
},
),
);
}
then in your home page widget
class HomePage extends StatefulWidget {
static const routeName = 'homePage';
Let me know if you need anything else
回答2:
I just fixed it.
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'screens/home_screen.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
final cameras = await availableCameras();
final camera = cameras.first;
runApp(MaterialApp(
title: 'OCR Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: HomePage(
title: 'OCR Text Recognition',
camera: camera,
),
));
}
回答3:
You should request permissions before calling on availableCameras. That's why the screen is blank. Below is my code for using Camera. Get permissions before you call availableCameras. Also go to your android manifest file in flutter app and add the permissions there as well.
import 'package:permission_handler/permission_handler.dart';
@override
void initState() {
super.initState();
getPermissions();
}
Future getPermissions() async {
Map<Permission, PermissionStatus> permissions =
await [
Permission.storage, Permission.camera, Permission.microphone
].request();
if (permissions[Permission.storage] == PermissionStatus.granted &&
permissions[Permission.camera] == PermissionStatus.granted &&
permissions[Permission.microphone] == PermissionStatus.granted) {
await getCameras();
}
}
Future getCameras() async {
cameras = await availableCameras();
controller = CameraController(cameras[0], ResolutionPreset.high);
controller.initialize().then((_) {
/*if (!mounted) {
return;
}
setState(() {});*/
setState(() {
isPermitted = true;
});
});
}
来源:https://stackoverflow.com/questions/61355629/flutter-android-app-shows-just-a-blank-screen