Flutter-Android应用程序仅显示空白屏幕

时间:2020-04-22 01:00:49

标签: android flutter dart

Android应用程序仅显示空白屏幕。下面是我的代码

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,
    ),
  ));
}

错误消息

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): 

3 个答案:

答案 0 :(得分:0)

您必须像这样定义通往HomePage的路线

runApp(
    MaterialApp(
      title: 'DEMO',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(
        title: 'DEMO',
        camera: camera,
      ),
      routes: {
        HomePage.routeName: (ctx) => HomePage(),
      },
    ),
  );
}

然后在您的首页窗口小部件中

class HomePage extends StatefulWidget {
  static const routeName = 'homePage'; 

让我知道您是否还需要其他东西

答案 1 :(得分:0)

在调用availableCameras之前,您应该请求权限。这就是屏​​幕空白的原因。以下是我使用Camera的代码。致电availableCameras之前,请先获取权限。还要在flutter应用中转到您的android清单文件,并在其中添加权限。

String qry2 = null;
Prepared statement ps2 = null;

qry2 = " UPDATE term1 SET Total_Score = sum(English + Social_Science + Science  + Maths + PE + MAL Arts) WHERE SID = ?";

ps2.=conn.prepareStatement(qry2);
Int res2=ps2.executeUpdate();

答案 2 :(得分:0)

我刚刚修复了它。

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,
    ),
  ));
}