我正在使用 streams 通过 streamBuilder 显示我的数据。问题是信息流给了我未来<列表> 快照,我需要使用 length 作为 itemCount 。 / p>
也使用 sqflite ,我从没见过两者的例子。
下面是我的代码;
import 'package:flutter/material.dart';
import 'package:password_safe/blocs/safe_card_bloc.dart';
import 'package:password_safe/models/SafeCard.dart';
class Body extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder(
initialData: safeCardBloc.getCards(),
stream: safeCardBloc.getStream,
builder: (context, snapshot) {
return snapshot.hasError
? Center(
child: Text(snapshot.error.toString()),
)
: snapshot.hasData
? buildSafeCardListItems(snapshot)
: Center(
child: Text("Veri yok"),
);
},
);
}
buildSafeCardListItems(AsyncSnapshot snapshot) {
return ListView.builder(
itemCount: snapshot.data.length, // problem
itemBuilder: (context, index) {
List<SafeCard> safeCards = snapshot.data;
return Padding(
padding: EdgeInsets.only(top: 8),
child: Card(
margin: EdgeInsets.fromLTRB(20, 6, 20, 0),
child: ListTile(
title: Text(
"Name: ${safeCards[index].name}",
style: TextStyle(
fontWeight: FontWeight.w600,
),
),
subtitle: Text(
"Password: ${safeCards[index].password}",
style: TextStyle(
fontWeight: FontWeight.w600,
),
),
),
),
);
},
);
}
}
类'Future >'没有实例获取器'length'。
接收方:“ Future
>”的实例
尝试致电:长度
import 'dart:async';
import 'package:password_safe/data/db_helper.dart';
import 'package:password_safe/models/SafeCard.dart';
class SafeCardBloc {
final safeCardStreamController = StreamController.broadcast();
Stream get getStream => safeCardStreamController.stream;
final dbHelper = DbHelper();
void addToSafe(SafeCard card) async {
dbHelper.insert(card);
safeCardStreamController.sink.add(dbHelper.getSafeCards());
}
Future<List<SafeCard>> getCards() async {
var cards = dbHelper.getSafeCards();
print(cards);
return cards;
}
}
final safeCardBloc = SafeCardBloc();
数据库类
import 'dart:async';
import 'package:password_safe/models/SafeCard.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
class DbHelper {
Database _db;
Future<Database> get db async {
if (_db == null) {
_db = await initializeDb();
}
return _db;
}
Future<Database> initializeDb() async {
String dbPath = join(await getDatabasesPath(), "safeCardDb.db");
var safeCardDb = openDatabase(dbPath, version: 1, onCreate: createDb);
return safeCardDb;
}
void createDb(Database db, int version) async {
await db.execute(
"Create table safeCards(id integer primary key autoincrement, name text, description text, password text)");
}
Future<List<SafeCard>> getSafeCards() async {
Database db = await this.db;
var result = await db.query("safeCards");
return List.generate(result.length, (i) {
return SafeCard.fromObject(result[i]);
});
}
Future<int> insert(SafeCard safeCard) async {
Database db = await this.db;
var result = await db.insert("safeCards", safeCard.toMap());
return result;
}
Future<int> delete(int id) async {
Database db = await this.db;
var result = await db.rawDelete("delete from safeCards where id= $id");
return result;
}
Future<int> update(SafeCard safeCard) async {
Database db = await this.db;
var result = await db.update("safeCards", safeCard.toMap(),
where: "id=?", whereArgs: [safeCard.id]);
return result;
}
}
答案 0 :(得分:0)
我认为“ snapshot.data”不是列表。所以,
来自
buildSafeCardListItems(AsyncSnapshot snapshot) {
return ListView.builder(
itemCount: snapshot.data.length, // problem
itemBuilder: (context, index) {
List<SafeCard> safeCards = snapshot.data;
到
buildSafeCardListItems(AsyncSnapshot snapshot) {
List<SafeCard> safeCards = snapshot.data;
return ListView.builder(
itemCount: safeCards.length,
itemBuilder: (context, index) {