在flutter資料庫示例代碼中:https ://docs.flutter.dev/cookbook/persistence/sqlite
此代碼塊引發 3 個錯誤:
// Convert the List<Map<String, dynamic> into a List<Dog>.
return List.generate(maps.length, (i) {
return Dog(
id: maps[i]['id'],
name: maps[i]['name'],
age: maps[i]['age'],
);
});
}
錯誤是:
- 引數型別“動態”不能分配給引數型別“int”。
- 引數型別“動態”不能分配給引數型別“字串”。
- 引數型別“動態”不能分配給引數型別“int”。
并且 vscode 突出顯示該行的“地圖”部分(地圖 [i])
我不知道該指南針對的是哪個版本的顫振,但我猜這可能是較新的顫振版本中的問題?我正在嘗試學習顫振以及如何使用 sqlite,所以這讓我陷入了困境。提前感謝您的任何意見!
作為參考,這是整個代碼塊:
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
void main() async {
// Avoid errors caused by flutter upgrade.
// Importing 'package:flutter/widgets.dart' is required.
WidgetsFlutterBinding.ensureInitialized();
// Open the database and store the reference.
final database = openDatabase(
// Set the path to the database. Note: Using the `join` function from the
// `path` package is best practice to ensure the path is correctly
// constructed for each platform.
join(await getDatabasesPath(), 'doggie_database.db'),
// When the database is first created, create a table to store dogs.
onCreate: (db, version) {
// Run the CREATE TABLE statement on the database.
return db.execute(
'CREATE TABLE dogs(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)',
);
},
// Set the version. This executes the onCreate function and provides a
// path to perform database upgrades and downgrades.
version: 1,
);
// Define a function that inserts dogs into the database
Future<void> insertDog(Dog dog) async {
// Get a reference to the database.
final db = await database;
// Insert the Dog into the correct table. You might also specify the
// `conflictAlgorithm` to use in case the same dog is inserted twice.
//
// In this case, replace any previous data.
await db.insert(
'dogs',
dog.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
// A method that retrieves all the dogs from the dogs table.
Future<List<Dog>> dogs() async {
// Get a reference to the database.
final db = await database;
// Query the table for all The Dogs.
final List<Map<String, dynamic>> maps = await db.query('dogs');
// Convert the List<Map<String, dynamic> into a List<Dog>.
return List.generate(maps.length, (i) {
return Dog(
id: maps[i]['id'],
name: maps[i]['name'],
age: maps[i]['age'],
);
});
}
Future<void> updateDog(Dog dog) async {
// Get a reference to the database.
final db = await database;
// Update the given Dog.
await db.update(
'dogs',
dog.toMap(),
// Ensure that the Dog has a matching id.
where: 'id = ?',
// Pass the Dog's id as a whereArg to prevent SQL injection.
whereArgs: [dog.id],
);
}
Future<void> deleteDog(int id) async {
// Get a reference to the database.
final db = await database;
// Remove the Dog from the database.
await db.delete(
'dogs',
// Use a `where` clause to delete a specific dog.
where: 'id = ?',
// Pass the Dog's id as a whereArg to prevent SQL injection.
whereArgs: [id],
);
}
// Create a Dog and add it to the dogs table
var fido = const Dog(
id: 0,
name: 'Fido',
age: 35,
);
await insertDog(fido);
// Now, use the method above to retrieve all the dogs.
print(await dogs()); // Prints a list that include Fido.
// Update Fido's age and save it to the database.
fido = Dog(
id: fido.id,
name: fido.name,
age: fido.age 7,
);
await updateDog(fido);
// Print the updated results.
print(await dogs()); // Prints Fido with age 42.
// Delete Fido from the database.
await deleteDog(fido.id);
// Print the list of dogs (empty).
print(await dogs());
}
class Dog {
const Dog({
required this.id,
required this.name,
required this.age,
});
final int id;
final String name;
final int age;
// Convert a Dog into a Map. The keys must correspond to the names of the
// columns in the database.
Map<String, dynamic> toMap() {
return {
'id': id,
'name': name,
'age': age,
};
}
// Implement toString to make it easier to see information about
// each dog when using the print statement.
@override
String toString() {
return 'Dog{id: $id, name: $name, age: $age}';
}
}
uj5u.com熱心網友回復:
嘗試這樣做 -
return List.generate(maps.length, (i) {
return Dog(
id: maps[i]['id'] as int,
name: maps[i]['name'] as String,
age: maps[i]['age'] as int,
);
});
}
uj5u.com熱心網友回復:
在您的 Dog 模型中創建以下方法
Dog.fromJson(Map<String, dynamic> json)
: id = json['id'],
name = json['name'],
age = json['age'];
用法:
return List.generate(maps.length, (i) {
return Dog.fromJson(maps[i]);
});
}
我遇到了同樣的錯誤,但這個解決方案對我有用,希望它也對你有用
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/525585.html
標籤:扑sqlite
上一篇:ReactNativeJS同步
