【问题标题】:Flutter - ImagePicker Issue颤振 - ImagePicker 问题
【发布时间】:2020-07-10 06:49:59
【问题描述】:
import 'package:flutter/material.dart';
import 'login_page.dart';
import 'sign_in.dart';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_ml_vision/firebase_ml_vision.dart';
import 'package:cloud_firestore/cloud_firestore.dart';

class FirstScreen extends StatelessWidget {
  var myDatabase = Firestore.instance;
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Center(child: Text('IT Interns are the best!!')),
        ),
        backgroundColor: Colors.white,
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Welcome to Krea\'s Official App!',
              textAlign: TextAlign.center,
              style: TextStyle(
                  fontWeight: FontWeight.w800,
                  fontSize: 40.0,
                  color: Colors.black),
            ),
            SizedBox(height: 30.0),
            Container(
              child: Image.asset('assets/krea.png'),
            ),
            SizedBox(
              height: 50.0,
              width: 250.0,
              child: Divider(color: Colors.blueAccent),
            ),
            Icon(
              Icons.face,
              size: 120,
            ),
            Text(
              'Akshay Jain',
              style: TextStyle(
                fontSize: 40.0,
                color: Colors.black,
                fontWeight: FontWeight.bold,
              ),
            ),
            Text(
              'Krea_StudentLife_Level_100',
              style: TextStyle(
                fontSize: 18.0,
                color: Colors.black,
                fontWeight: FontWeight.normal,
                letterSpacing: 1.0,
              ),
            ),
            SizedBox(height: 20.0),
            RaisedButton(
                onPressed: () {
                  ImagePicker.getImage(source: ImageSource.camera)
                      .then((photo) {
                    BarcodeDetector detector = FirebaseVision.instance
                        .barcodeDetector(BarcodeDetectorOptions(
                            barcodeFormats: BarcodeFormat.qrCode));
                    detector
                        .detectInImage(FirebaseVisionImage.fromFile(photo))
                        .then((barcodes) {
                      if (barcodes.length > 0) {
                        var barcode = barcodes[0]; // Pick first barcode

                        myDatabase.collection("qr_codes").add({
                          "raw_data": barcode.rawValue,
                          "time": new DateTime.now().millisecondsSinceEpoch
                        }).then((_) {
                          print("One document added.");
                        });

                        showDialog(
                            context: context,
                            builder: (context) {
                              return new AlertDialog(
                                title: new Text("QR Code Contents"),
                                content: new Text(barcode.rawValue),
                                actions: <Widget>[
                                  new FlatButton(
                                      onPressed: () {
                                        Navigator.of(context).pop();
                                      },
                                      child: new Text("OK"))
                                ],
                              );
                            });
                      }
                    });
                  });
                },
                child: new Text("Capture QR Code")),
            FloatingActionButton.extended(
              onPressed: () {
                signOutGoogle();
                Navigator.of(context).pushAndRemoveUntil(
                    MaterialPageRoute(builder: (context) {
                  return LoginPage();
                }), ModalRoute.withName('/'));
              },
              backgroundColor: Colors.blue,
              icon: Icon(Icons.lock),
              label: Text('Sign Out'),
            ),
          ],
        ),
      ),
    );
  }
}

我们得到的错误是在第 62 行它说不能使用静态访问来访问实例成员“getImage”。这是整个代码失败的地方。这似乎是一个包装问题,但由于 getImage 行,二维码没有被处理。

【问题讨论】:

标签: flutter dart imagepicker


【解决方案1】:

错误消息是不言自明的。将 ImagePicker.getImage 替换为实例调用,而不是像

这样的静态类调用
var picker = ImagePicker;
picker.getImage(...)

【讨论】:

    【解决方案2】:

    首先你需要获取 ImagePicker() 的实例:

    class FirstScreen extends StatelessWidget {
      var picker = ImagePicker();
    

    然后在你的按钮点击使用这个创建的实例:

    picker.getImage(source: ImageSource.camera).then((photo) {});
    

    【讨论】:

    • 嘿!它有效,但我收到错误错误:无法将参数类型“PickedFile”分配给参数类型“文件”。 (argument_type_not_assignable at [krea_app_security] lib\first_screen.dart:68)你怎么看?
    • @AppDevelopment101 请分享您的代码和您的 image_picker 依赖项版本号。
    • image_picker: ^0.6.7+4,我的代码是一样的,只是在 var myDatabase 上面的第 9 行添加了这个,并将第 63 行更改为 picker.getImage(source: ImageSource.camera).then( (照片){ BarcodeDetector 检测器 = FirebaseVision.instance
    • 嘿,同样的运气吗?
    • @AppDevelopment101 更改为:FirebaseVisionImage.fromFile(File(photo.path))
    猜你喜欢
    • 2021-11-16
    • 2021-05-05
    • 2022-01-12
    • 2021-10-20
    • 1970-01-01
    • 2020-09-26
    • 2019-05-25
    • 2021-07-28
    • 2020-08-03
    相关资源
    最近更新 更多