-1
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 行,没有处理 QR 码。

4

2 回答 2

1

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

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

然后在您的按钮中单击使用此创建的实例:

picker.getImage(source: ImageSource.camera).then((photo) {});
于 2020-07-10T13:40:17.133 回答
1

错误消息是不言自明的。用实例调用而不是静态类调用替换 ImagePicker.getImage

var picker = ImagePicker;
picker.getImage(...)
于 2020-07-10T07:06:20.603 回答