Reputation: 17321
i'm new in Hive
database in flutter and i'm trying to figure out why i can't use Hive.openBox
inside FutureBuilder
?
FutureBuilder(
future: Hive.openBox<User>('userBox'),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
final User userAccount = snapshot.data;
if (userAccount == null) {
return Center(
child: Container(
child: Text('LOGIN'),
));
} else {
return Center(
child: Container(
child: Text('SUCCESS'),
));
}
}
return Container(
width: 0.0,
height: 0.0,
);
},
);
i get this error:
type 'BoxImpl' is not a subtype of type 'User'
but this code work fine in main
function:
var box = await Hive.openBox<User>('userBox');
User
class:
@HiveType(typeId: 1)
class User{
@HiveField(0)
String username;
@HiveField(1)
String password;
@HiveField(2)
String mobileNumber;
@HiveField(3)
String biography;
@HiveField(4)
String avatar;
@HiveField(5)
String website;
@HiveField(6)
String linkedinLink;
@HiveField(7)
String githubLink;
@HiveField(8)
String facebookLink;
@HiveField(9)
String telegramLink;
@HiveField(10)
String twitterLink;
@HiveField(11)
PageInfo pageInfo;
User(this.username, this.password, this.mobileNumber, this.biography, this.avatar, this.website, this.linkedinLink, this.githubLink,
this.facebookLink, this.telegramLink, this.twitterLink, this.pageInfo);
}
main.dart
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
var appDocumentDirectory = await path_provider.getApplicationDocumentsDirectory();
Hive
..init(appDocumentDirectory.path)
..registerAdapter(UserAdapter())
..registerAdapter(PageInfoAdapter());
runApp(MultiProvider(providers: providers, child: OKToast(child: StartupApplication())));
}
Upvotes: 0
Views: 1411
Reputation: 17321
Problem Solved, that was casting user inside FutureBuilder
builder: (context, AsyncSnapshot<Box<User>> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
final Box<User> userAccount = snapshot.data;
if (userAccount.isEmpty) {
return Center(
child: Container(
child: Text('LOGIN',style: TextStyle(color: Colors.white),),
));
} else {
return Center(
child: Container(
child: Text('SUCCESS',style: TextStyle(color: Colors.white),),
));
}
}
return Container(
width: 0.0,
height: 0.0,
);
},
Upvotes: 2
Reputation: 5182
replace this
..init(appDocumentDirectory.path)
with
..initFlutter(appDocumentDirectory.path);
Upvotes: 0