This is my stateful widget and flutter keeps throwing an exception and I don't know why.
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: FutureBuilder(
future: createUser(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return const Text('Creating Account....');
default:
if (snapshot.hasError) {
return Text('Error: ${snapshot.error}');
} else {
return Home(json: snapshot.data);
}
}
})));
}
and the Home
Widget:
const Home({Key? key, required this.json}) : super(key: key);
final String json;
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
return Text(widget.json);
}
}
Everything looks okay but when I run the build, i get an exception:
type 'Response<dynamic>' is not a subtype of type 'String'
The relevant error-causing widget was
FutureBuilder<dynamic>
This is the Create method:
Future createUser() async {
final prefs = await sharedPref();
final user = User((b) => b
..firstName = prefs.getString('firstName')
..lastName = prefs.getString('lastName')
..email = prefs.getString('email')
..profilePhotoUrl = prefs.getString('photo')
..isocode = prefs.getString('currency'));
final userSerlizedObject = standardSerializers.serialize(user);
final res = devHttpClient.post(
'/createaccount',
data: userSerlizedObject,
);
return res;
}
What is causing this exception, I am having trouble finding why?
CodePudding user response:
I guess that your method createUser()
returns a Response<T>
.
When calling this method
return Home(json: snapshot.data);
You're casting a Response<T>
in a String
(since your json
param is a String)
You should update your createUser()
method in order to return the body of the response, and not the response itself
CodePudding user response:
just add .toString() to snapshot.data
return Home(json: snapshot.data.toString());