Flutter & Sqflite:MissingPluginException(未在通道 com.tekartik.sqflite 上找到方法 getDatabasesPath 的实现)
Flutter & Sqflite: MissingPluginException(No implementation found for method getDatabasesPath on channel com.tekartik.sqflite)
我目前正在尝试在我的 flutter 应用程序中创建一个数据库。
在 database.dart 文件中我有:
void CreateBD() async{
WidgetsFlutterBinding.ensureInitialized();
final database = openDatabase(
//on choisi le path de la bd
join(await getDatabasesPath(), 'hyrule_database.db'),
//initialisation des tables
onCreate: (db, version) {
// Run the CREATE TABLE statement on the database.
return db.execute(
'CREATE TABLE foods(id INTEGER PRIMARY KEY, name TEXT,image TEXT, description TEXT, cookingEffect TEXT, category TEXT, commonLocations list<TEXT>, heartsRecovered INTEGER);'
'CREATE TABLE nonfoods(id INTEGER PRIMARY KEY, name TEXT,image TEXT, description TEXT,category TEXT, commonLocations list<TEXT>, drops list<TEXT>);'
'CREATE TABLE equipments(id INTEGER PRIMARY KEY, name TEXT,image TEXT, description TEXT, attack INTEGER, category TEXT, commonLocations list<TEXT>, defense INTEGER);'
'CREATE TABLE materials(id INTEGER PRIMARY KEY, name TEXT,image TEXT, description TEXT, cookingEffect TEXT, category TEXT, commonLocations list<TEXT>, heartsRecovered INTEGER);'
'CREATE TABLE monsters(id INTEGER PRIMARY KEY, name TEXT,image TEXT, description TEXT, category TEXT, commonLocations list<TEXT>, drops list<TEXT>);'
'CREATE TABLE treasure(id INTEGER PRIMARY KEY, name TEXT,image TEXT, description TEXT, category TEXT, commonLocations list<TEXT>, drops list<TEXT>)',
);
},
//version afin de laisser la posibilité de upgrade et downgrade
version: 1,
);
}
Future<Database> connectBD()async{
final database = openDatabase(
join(await getDatabasesPath(), 'hyrule_database.db'),
);
return database;
}
我为每个 table 创建了 CRUD 函数,这里是食物的例子 table:
// Insertion
Future<void> insertFood(Food food) async {
final db = await connectBD();
await db.insert(
'foods',
food.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
//récupération
Future<List<Food>> foods() async {
final db = await connectBD();
final List<Map<String, dynamic>> maps = await db.query('foods');
return List.generate(maps.length, (i) {
return Food(
id: maps[i]['id'],
name: maps[i]['name'],
image: maps[i]['image'],
heartsRecovered: maps[i]['hearts_recovered'],
description: maps[i]['description'],
cookingEffect: maps[i]['cooking_effect'],
commonLocations: maps[i]['common_locations'],
category: maps[i]['category'],
);
});
}
//update
Future<void> updateFood(Food food) async {
final db = await connectBD();
await db.update(
'foods',
food.toMap(),
where: 'id = ?',
whereArgs: [food.id],
);
}
//delete
Future<void> deleteFood(int id) async {
final db = await connectBD();
await db.delete(
'foods',
where: 'id = ?',
whereArgs: [id],
);
}
然后我尝试执行创建,但最终出现错误:
错误:MissingPluginException(未在通道 com.tekartik.sqflite 上找到方法 getDatabasesPath 的实现)
我用来执行的文件看起来像:
import 'package:flutter/material.dart';
import 'package:projet_flutter/Utils/hyrule_bd.dart';
import 'package:projet_flutter/Utils/fill_bd.dart';
import 'package:projet_flutter/models/food_model.dart';
import 'dart:async';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
class TestApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => _TestState();
}
class _TestState extends State<TestApp> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
CreateBD();
//remplirBD();
return MaterialApp(
title: 'Fetch Data Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Fetch Data Example'),
),
body: Center(
child:FutureBuilder<List<Food>>(
future: foods(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text('${snapshot.data}');
} else if (snapshot.hasError) {
print("erreur test: ${snapshot.error}");
return Text('${snapshot.error}');
}
// By default, show a loading spinner.
return const CircularProgressIndicator();
},
),
),
));
}
}
我的 pubspec.yaml :
name: projet_flutter
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.14.0 <3.0.0"
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
flutter:
sdk: flutter
http: 0.13.4
sqflite: any
path: any
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^1.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
我目前正在学习 Flutter 开发,所以我想知道哪里出了问题?
有时在将新的依赖项添加到 pubspec 文件后会发生这种情况。
从项目终端尝试运行以下命令:
flutter clean
然后 re-run 项目。
Sqflite 不支持网络 (https://github.com/tekartik/sqflite),这是我试图构建的。
我目前正在尝试在我的 flutter 应用程序中创建一个数据库。
在 database.dart 文件中我有:
void CreateBD() async{
WidgetsFlutterBinding.ensureInitialized();
final database = openDatabase(
//on choisi le path de la bd
join(await getDatabasesPath(), 'hyrule_database.db'),
//initialisation des tables
onCreate: (db, version) {
// Run the CREATE TABLE statement on the database.
return db.execute(
'CREATE TABLE foods(id INTEGER PRIMARY KEY, name TEXT,image TEXT, description TEXT, cookingEffect TEXT, category TEXT, commonLocations list<TEXT>, heartsRecovered INTEGER);'
'CREATE TABLE nonfoods(id INTEGER PRIMARY KEY, name TEXT,image TEXT, description TEXT,category TEXT, commonLocations list<TEXT>, drops list<TEXT>);'
'CREATE TABLE equipments(id INTEGER PRIMARY KEY, name TEXT,image TEXT, description TEXT, attack INTEGER, category TEXT, commonLocations list<TEXT>, defense INTEGER);'
'CREATE TABLE materials(id INTEGER PRIMARY KEY, name TEXT,image TEXT, description TEXT, cookingEffect TEXT, category TEXT, commonLocations list<TEXT>, heartsRecovered INTEGER);'
'CREATE TABLE monsters(id INTEGER PRIMARY KEY, name TEXT,image TEXT, description TEXT, category TEXT, commonLocations list<TEXT>, drops list<TEXT>);'
'CREATE TABLE treasure(id INTEGER PRIMARY KEY, name TEXT,image TEXT, description TEXT, category TEXT, commonLocations list<TEXT>, drops list<TEXT>)',
);
},
//version afin de laisser la posibilité de upgrade et downgrade
version: 1,
);
}
Future<Database> connectBD()async{
final database = openDatabase(
join(await getDatabasesPath(), 'hyrule_database.db'),
);
return database;
}
我为每个 table 创建了 CRUD 函数,这里是食物的例子 table:
// Insertion
Future<void> insertFood(Food food) async {
final db = await connectBD();
await db.insert(
'foods',
food.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
//récupération
Future<List<Food>> foods() async {
final db = await connectBD();
final List<Map<String, dynamic>> maps = await db.query('foods');
return List.generate(maps.length, (i) {
return Food(
id: maps[i]['id'],
name: maps[i]['name'],
image: maps[i]['image'],
heartsRecovered: maps[i]['hearts_recovered'],
description: maps[i]['description'],
cookingEffect: maps[i]['cooking_effect'],
commonLocations: maps[i]['common_locations'],
category: maps[i]['category'],
);
});
}
//update
Future<void> updateFood(Food food) async {
final db = await connectBD();
await db.update(
'foods',
food.toMap(),
where: 'id = ?',
whereArgs: [food.id],
);
}
//delete
Future<void> deleteFood(int id) async {
final db = await connectBD();
await db.delete(
'foods',
where: 'id = ?',
whereArgs: [id],
);
}
然后我尝试执行创建,但最终出现错误:
错误:MissingPluginException(未在通道 com.tekartik.sqflite 上找到方法 getDatabasesPath 的实现)
我用来执行的文件看起来像:
import 'package:flutter/material.dart';
import 'package:projet_flutter/Utils/hyrule_bd.dart';
import 'package:projet_flutter/Utils/fill_bd.dart';
import 'package:projet_flutter/models/food_model.dart';
import 'dart:async';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
class TestApp extends StatefulWidget {
@override
State<StatefulWidget> createState() => _TestState();
}
class _TestState extends State<TestApp> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
CreateBD();
//remplirBD();
return MaterialApp(
title: 'Fetch Data Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: const Text('Fetch Data Example'),
),
body: Center(
child:FutureBuilder<List<Food>>(
future: foods(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text('${snapshot.data}');
} else if (snapshot.hasError) {
print("erreur test: ${snapshot.error}");
return Text('${snapshot.error}');
}
// By default, show a loading spinner.
return const CircularProgressIndicator();
},
),
),
));
}
}
我的 pubspec.yaml :
name: projet_flutter
description: A new Flutter project.
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.14.0 <3.0.0"
# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
# consider running `flutter pub upgrade --major-versions`. Alternatively,
# dependencies can be manually updated by changing the version numbers below to
# the latest version available on pub.dev. To see which dependencies have newer
# versions available, run `flutter pub outdated`.
dependencies:
flutter:
sdk: flutter
http: 0.13.4
sqflite: any
path: any
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
# package. See that file for information about deactivating specific lint
# rules and activating additional ones.
flutter_lints: ^1.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
uses-material-design: true
# To add assets to your application, add an assets section, like this:
# assets:
# - images/a_dot_burr.jpeg
# - images/a_dot_ham.jpeg
# An image asset can refer to one or more resolution-specific "variants", see
# https://flutter.dev/assets-and-images/#resolution-aware.
# For details regarding adding assets from package dependencies, see
# https://flutter.dev/assets-and-images/#from-packages
# To add custom fonts to your application, add a fonts section here,
# in this "flutter" section. Each entry in this list should have a
# "family" key with the font family name, and a "fonts" key with a
# list giving the asset and other descriptors for the font. For
# example:
# fonts:
# - family: Schyler
# fonts:
# - asset: fonts/Schyler-Regular.ttf
# - asset: fonts/Schyler-Italic.ttf
# style: italic
# - family: Trajan Pro
# fonts:
# - asset: fonts/TrajanPro.ttf
# - asset: fonts/TrajanPro_Bold.ttf
# weight: 700
#
# For details regarding fonts from package dependencies,
# see https://flutter.dev/custom-fonts/#from-packages
我目前正在学习 Flutter 开发,所以我想知道哪里出了问题?
有时在将新的依赖项添加到 pubspec 文件后会发生这种情况。
从项目终端尝试运行以下命令:
flutter clean
然后 re-run 项目。
Sqflite 不支持网络 (https://github.com/tekartik/sqflite),这是我试图构建的。