I want to get the value of a radio button group which is stored in a variable called currentOption
in my Home
class, i want to pass the value to my Database
class . My issue is when I pass currentOption
variable to my Database
class and print it in the database class the value always stays the same as the initial value it has in the Home
class even after I toggle the the radio button to trigger the value change, but when i print it in the Home
class the value of currentOption
changes to the value of the radio button I toggled.
Home.dart
import 'package:flutter/material.dart';
import 'package:watch_hub/services/auth.dart';
import 'package:watch_hub/services/database.dart';
import 'package:provider/provider.dart';
import 'package:watch_hub/screens/home/watch_list.dart';
import 'package:watch_hub/models/watch.dart';
List<dynamic> options = [0, 'Rolex', 'Longines'];
class Home extends StatefulWidget {
Home({super.key});
dynamic currentOption = options[0]; //THIS IS THE VARIABLE I WANT TO PASS TO ANOTHER CLASS
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
final AuthService _auth = AuthService();
@override
Widget build(BuildContext context) {
return StreamProvider<List<Watch>>.value(
value: DatabaseService().watches,
initialData: const [],
child: Scaffold(
backgroundColor: Colors.brown[50],
drawer: Drawer(
elevation: 20.0,
child: Column(
children: <Widget>[
UserAccountsDrawerHeader(
accountName: Text("Flutter"),
accountEmail: null,
),
//VALUE OF currentOption CHANGES WHEN I TOGGLE EITHER OF THESE CHECKBOXES
ListTile(
title: const Text('None'),
leading: Radio<dynamic>(
value: options[0],
groupValue: widget.currentOption,
onChanged: (value) {
setState(() {
widget.currentOption = value;
print("current is ${widget.currentOption}");
});
},
),
),
Divider(
height: 0.1,
),
ListTile(
title: const Text('Rolex'),
leading: Radio(
value: options[1],
groupValue: widget.currentOption,
onChanged: (value) {
setState(() {
widget.currentOption = value;
print("current is ${widget.currentOption}");
});
},
)),
ListTile(
title: const Text('Longines'),
leading: Radio(
value: options[2],
groupValue: widget.currentOption,
onChanged: (value) {
setState(() {
widget.currentOption = value;
print("current is ${widget.currentOption}");
});
},
)),
ListTile(
title: Text("Promotions"),
leading: Icon(Icons.local_offer),
),
],
),
),
appBar: AppBar(
title: Text("Watch Hub"),
backgroundColor: Colors.brown[400],
elevation: 0.0,
actions: <Widget>[
TextButton.icon(
icon: Icon(Icons.person),
label: Text("Logout"),
onPressed: () async {
await _auth.signOut();
},
)
],
),
body: const WatchList(),
),
);
}
}
database.dart
import "package:cloud_firestore/cloud_firestore.dart";
import "package:watch_hub/models/watch.dart";
import 'package:watch_hub/screens/home/home.dart';
class DatabaseService {
final String? uid;
DatabaseService({this.uid});
//collection reference
final CollectionReference users =
FirebaseFirestore.instance.collection("Users");
final CollectionReference watch_list =
FirebaseFirestore.instance.collection("watch_hub");
dynamic brand = "Longines";
dynamic model = "Aviation Big Eye";
Future setUserData(
String email, String address, String fullName, String phone) async {
return await users.doc(uid).set({
"email": email,
"address": address,
"fullName": fullName,
"phone": phone,
});
}
//watch list from snapshot
List<Watch> _watchListFromSnapshot(QuerySnapshot snapshot) {
print("without $option");
return snapshot.docs.map((doc) {
return Watch(
id: doc['id'] ?? '',
brand: doc['brand'] ?? '',
model: doc['model'] ?? '',
type: doc['type'] ?? '',
popularity: doc['popularity'] ?? 0,
price: doc['price'].toString(),
image: doc['image'] ?? '',
);
}).toList();
}
//get watches list
Stream<List<Watch>> get watches {
dynamic option = Home().currentOption;
/* WHEN I PRINT THIS THE VALUE OS option IS ALWAYS 0 BECAUSE THAT WAS ITS INITIAL VALUE IN home.dart */
print("option is $option");
late Query filter;
option is String
? filter = watch_list.where("brand", isEqualTo: option)
: filter = watch_list.where("brand", isNotEqualTo: true);
print(watch_list);
return filter.snapshots().map(_watchListFromSnapshot);
}
}