我收到以下与我的 Firebase 代码相关的错误,以保存图像、日期和废物数量。或者这可能是与我的 Firebase 配置相关的错误?
import 'dart:io';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:async';
import 'package:wasteagram/model/waste.dart';
import 'package:path/path.dart' as path;
import 'package:uuid/uuid.dart';
class CameraScreen extends StatefulWidget {
final bool isUpdating;
CameraScreen({this.isUpdating = true});
@override
_CameraScreenState createState() => _CameraScreenState();
}
class _CameraScreenState extends State<CameraScreen> {
final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
Waste _currentWaste = Waste();
Widget _buildDateField() {
return Form(
key: _formKey,
child: TextFormField(
decoration: InputDecoration(labelText: 'Date'),
keyboardType: TextInputType.text,
style: TextStyle(fontSize: 20),
validator: (String value) {
if(value.isEmpty){
return 'Date required';
}
if(value.length < 3 || value.length > 20) {
return 'Name must be more than 3 or less than 20';
}
return null;
},
onSaved: (String value) {
_currentWaste.wastedate = value;
},
),
);
}
Widget _buildWasteNumber() {
return Form(
child: TextFormField(
decoration: InputDecoration(labelText: 'Number'),
keyboardType: TextInputType.number,
style: TextStyle(fontSize: 20),
validator: (value) {
if(value.isEmpty){
return 'Number required';
}
return null;
},
onSaved: (String value) {
String wasteNum = _currentWaste.wastenumber.toString();
wasteNum = value;
},
),
);
}
_saveWaste(context) {
print("saveWaste Called");
if(!_formKey.currentState.validate()) {
return "FALSE";
}
_formKey.currentState.save();
print("form saved");
uploadItems(_currentWaste, widget.isUpdating, image);
print("date ${_currentWaste.wastedate}");
print("number ${_currentWaste.wastenumber.toString()}");
print("_imageFile ${image.toString()}");
}
File image;
void getImage() async {
image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState( () {});
}
@override
Widget build(BuildContext context) {
if (image == null) {
return Scaffold(
appBar: AppBar(
title: Text('Wasteagram')
),
body: Center(
child: RaisedButton(
child: Text('Select Photo'),
onPressed: () {
getImage();
},
),
),
);
} else {
return Scaffold(
appBar: AppBar(
title: Text('Wasteagram')
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Image.file(image),
SizedBox(height: 40),
RaisedButton(
child: Text('Select Photo'),
onPressed: () {
getImage();
}
),
_buildDateField(),
_buildWasteNumber(),
],
),
floatingActionButton: FloatingActionButton(
onPressed: () => _saveWaste(context),
child: Icon(Icons.save),
foregroundColor: Colors.white,
),
);
}
}
}
uploadItems(Waste waste, bool isUpdating, File localFile) async {
if (localFile != null) {
print("uploading image");
var fileExtension = path.extension(localFile.path);
print(fileExtension);
var uuid = Uuid().v4();
final StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child('/$uuid$fileExtension');
await firebaseStorageRef.putFile(localFile).onComplete.catchError(
(onError){
print(onError);
return false;
}
);
String url = await firebaseStorageRef.getDownloadURL();
print("download url: $url");
_uploadWaste(waste, isUpdating, imageUrl: url);
} else {
print("skipping image upload");
_uploadWaste(waste, isUpdating);
}
}
_uploadWaste(Waste waste, bool isUpdating, {String imageUrl}) async {
CollectionReference wasteRef = Firestore.instance.collection('todolist');
if(imageUrl != null) {
waste.image = imageUrl;
}
if(isUpdating) {
waste.updatedAt = Timestamp.now();
await wasteRef.document(waste.id).updateData(waste.toMap());
print("updated waste with id: ${waste.id}");
} else {
DocumentReference documentRef = await wasteRef.add(waste.toMap());
waste.id = documentRef.documentID;
print("uploaded waste successfully: ${waste.toString()}");
await documentRef.setData(waste.toMap(), merge: true);
}
}
错误如下 -
E/StorageUtil(6584):获取 token 时出错 java.util.concurrent.ExecutionException:b.a.d.p.d.a:请先登录,然后再尝试获取 token 。
W/NetworkRequest(6584):请求没有身份验证 token
最佳答案
检查所有这些
1)更新到最新的依赖版本后,确保推送通知仍然按预期工作。
2) 查看 Firebase 安装文档。此外,请确保使用#onNewToken 实现监控 FCM 注册 token 生成。
3) 使用 Firebase 自动初始化过程和 Gradle 插件将 google-services.json 转换为资源的应用不受影响。但是,创建自己的 FirebaseOptions 实例的应用必须提供有效的 API key 、Firebase 项目 ID 和应用 ID。
关于firebase - E/StorageUtil(6584) : error getting token java. util.concurrent.ExecutionException : b. a.d.p.d.a:请先登录,然后再尝试获取 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60680216/