问题
I've been trying to make an offline app using json for local storage in flutter. I've used a package path_provider
to get current directory app apk. Here's the code:-
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'dart:io';
import 'dart:convert';
import 'package:path_provider/path_provider.dart';
class WorkersLog extends StatefulWidget {
@override
_WorkersLogState createState() => _WorkersLogState();
}
class _WorkersLogState extends State<WorkersLog> {
@override
Widget build(BuildContext context) {
void _showSettingsPanel() {
showModalBottomSheet(
isScrollControlled: true,
context: context,
builder: (context) {
return Container(
height: MediaQuery.of(context).size.height * .90,
padding: EdgeInsets.symmetric(vertical: 20.0, horizontal: 40.0),
child: Center(child: WorkerCardData()),
);
});
}
return Container(
child: Column(
children: [
SizedBox(height: 40),
Text(
"hello there"),
Row(
children: <Widget>[
Center(
child: Column(
children: [
SizedBox(height: 370),
Row(
children: [
SizedBox(width: 200),
FloatingActionButton.extended(
label: Text('add document'),
icon: Icon(Icons.save),
backgroundColor: Colors.greenAccent,
elevation: 5.0,
onPressed: () {
print('object');
_showSettingsPanel();
},
),
],
),
],
),
),
],
),
],
),
);
}
}
class WorkerCardData extends StatefulWidget {
@override
_WorkerCardDataState createState() => _WorkerCardDataState();
}
class _WorkerCardDataState extends State<WorkerCardData> {
TextEditingController keyInputController = new TextEditingController();
TextEditingController valueInputController = new TextEditingController();
File jsonFile;
Directory dir;
String fileName = "myFile.json";
bool fileExists = false;
Map<String, dynamic> fileContent;
@override
void initState() {
super.initState();
getApplicationDocumentsDirectory().then((Directory directory) {
dir = directory;
jsonFile = new File(dir.path + "/" + fileName);
fileExists = jsonFile.existsSync();
if (fileExists)
this.setState(
() => fileContent = json.decode(jsonFile.readAsStringSync()));
});
}
@override
void dispose() {
keyInputController.dispose();
valueInputController.dispose();
super.dispose();
}
void createFile(
Map<String, dynamic> content, Directory dir, String fileName) {
print("Creating file!");
File file = new File(dir.path + "/" + fileName);
file.createSync();
fileExists = true;
file.writeAsStringSync(json.encode(content));
}
void writeToFile(String key, dynamic value) {
print("Writing to file!");
Map<String, dynamic> content = {key: value};
if (fileExists) {
print("File exists");
Map<String, dynamic> jsonFileContent =
json.decode(jsonFile.readAsStringSync());
jsonFileContent.addAll(content);
jsonFile.writeAsStringSync(json.encode(jsonFileContent));
} else {
print("File does not exist!");
createFile(content, dir, fileName);
}
this.setState(() => fileContent = json.decode(jsonFile.readAsStringSync()));
print(fileContent);
}
@override
Widget build(BuildContext context) {
return Container(
child: Column(
children: [
new Padding(padding: new EdgeInsets.only(top: 10.0)),
new Text(
"File content: ",
style: new TextStyle(fontWeight: FontWeight.bold),
),
new Text(fileContent.toString()),
new Padding(padding: new EdgeInsets.only(top: 10.0)),
new Text("Add to JSON file: "),
new TextField(
controller: keyInputController,
),
new TextField(
controller: valueInputController,
),
new Padding(padding: new EdgeInsets.only(top: 20.0)),
new RaisedButton(
child: new Text("Add key, value pair"),
onPressed: () =>
writeToFile(keyInputController.text, valueInputController.text),
)
],
),
);
}
}
But the app crashes as soon as it launches. Here's what's written in debug console:-
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
✓ Built build\app\outputs\flutter-apk\app-debug.apk.
E/AndroidRuntime(10874): FATAL EXCEPTION: main
E/AndroidRuntime(10874): Process: com.illuminate.comcrop, PID: 10874
E/AndroidRuntime(10874): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/internal/Preconditions;
E/AndroidRuntime(10874): at com.google.firebase.provider.FirebaseInitProvider.checkContentProviderAuthority(com.google.firebase:firebase-common@@19.3.0:64)
E/AndroidRuntime(10874): at com.google.firebase.provider.FirebaseInitProvider.attachInfo(com.google.firebase:firebase-common@@19.3.0:44)
E/AndroidRuntime(10874): at android.app.ActivityThread.installProvider(ActivityThread.java:7545)
E/AndroidRuntime(10874): at android.app.ActivityThread.installContentProviders(ActivityThread.java:7090)
E/AndroidRuntime(10874): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6978)
E/AndroidRuntime(10874): at android.app.ActivityThread.access$1300(ActivityThread.java:270)
E/AndroidRuntime(10874): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2051)
Why is the error being caused.And how do I fix it?
I have no experience in java or android so It would be helpful if answer are with instructions from flutter coding perspective.
来源:https://stackoverflow.com/questions/63728525/flutter-app-crashes-after-using-package-path-provider-how-do-i-fix-it-in-flutte