My same code works with my other project but not this one?
This is my Form Screen which uses the same exact method as my previous project and it worked fine in the beginning but doesn't work now.
I am posting form data to a discord webhook which I have removed of course, upon saving the form I am calling the function which posts to the discord URL, it used to work fine in my previous project.
Check the bottom of the code to see where I am calling the function
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:relationship/services/data_model.dart';
void main() {
runApp(FormClass());
}
class FormClass extends StatefulWidget {
@override
State<StatefulWidget> createState() => _FormState();
}
class _FormState extends State<FormClass> {
final _formKey = GlobalKey<FormState>();
final _user = DataModel();
@override
void initState() {
super.initState();
}
double sliderValA = 0.0;
double sliderValB = 0.0;
double sliderValC = 0.0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Personal Log')),
body: Container(
padding: EdgeInsets.symmetric(horizontal: 10),
child: Builder(
builder: (context) => Form(
key: _formKey,
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
SizedBox(
height: 20,
),
TextFormField(
decoration: InputDecoration(
labelText: "Enter Location",
border: OutlineInputBorder()),
validator: (value) {
if (value!.isEmpty) {
return 'Enter Location !!!';
}
return null;
},
onSaved: (val) =>
setState(() => _user.location = val!),
),
SizedBox(
height: 20,
),
TextFormField(
decoration: InputDecoration(
labelText: "Enter Logs",
border: OutlineInputBorder()),
validator: (value) {
if (value!.isEmpty) {
return 'Enter Logs !!!';
}
return null;
},
onSaved: (val) => setState(() => _user.log = val!),
),
SizedBox(
height: 20,
),
Slider(
value: sliderValA,
min: 0,
max: 5,
divisions: 5,
label: "Alertness",
onChanged: (val) {
setState(() {
sliderValA = val;
_user.alertness = val;
});
}),
SizedBox(
height: 20,
),
Slider(
value: sliderValB,
min: 0,
max: 5,
divisions: 5,
label: "Hunger",
onChanged: (val) {
setState(() {
sliderValB = val;
_user.hunger = val;
});
}),
SizedBox(
height: 20,
),
Slider(
value: sliderValC,
min: 0,
max: 5,
divisions: 5,
label: "Thirst",
onChanged: (val) {
setState(() {
sliderValC = val;
_user.thirst = val;
});
}),
SizedBox(
height: 20,
),
Container(
child: Text('Achievments'),
),
SizedBox(
height: 20,
),
CheckboxListTile(
title: const Text('Job'),
value: _user.achievments[DataModel.job],
onChanged: (val) {
setState(() =>
_user.achievments[DataModel.job] = val!);
}),
CheckboxListTile(
title: const Text('Side Project'),
value: _user.achievments[DataModel.sideP],
onChanged: (val) {
setState(() =>
_user.achievments[DataModel.sideP] = val!);
}),
CheckboxListTile(
title: const Text('Meal'),
value: _user.achievments[DataModel.meal],
onChanged: (val) {
setState(() =>
_user.achievments[DataModel.meal] = val!);
}),
CheckboxListTile(
title: const Text('Exercise'),
value: _user.achievments[DataModel.exercise],
onChanged: (val) {
setState(() => _user
.achievments[DataModel.exercise] = val!);
}),
CheckboxListTile(
title: const Text('Social'),
value: _user.achievments[DataModel.social],
onChanged: (val) {
setState(() =>
_user.achievments[DataModel.social] = val!);
}),
CheckboxListTile(
title: const Text('Study'),
value: _user.achievments[DataModel.study],
onChanged: (val) {
setState(() =>
_user.achievments[DataModel.study] = val!);
}),
SizedBox(
height: 20,
),
Container(
// ignore: deprecated_member_use
child: RaisedButton(
onPressed: () {
final form = _formKey.currentState;
if (form!.validate()) {
form.save();
_user.makePostRequest();
}
},
child: Text('Save'))),
]))))));
}
}
This is my main.dart class
import 'package:flutter/material.dart';
import 'package:relationship/screens/form_screen.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
home: FormClass(),
);
}
}
This is the method
makePostRequest() async {
final uri = Uri.parse(
'My URL');
final header = {'Content-Type': 'application/json'};
Map<String, dynamic> body = {
"content": json.encode([
DateTime.now(),
location,
log,
achievments,
"Alertness $alertness",
"Hunger $hunger",
"Thirst $thirst"
]) // Change this line
};
String jsonBody = json.encode(body);
Response response = await post(
uri,
headers: header,
body: jsonBody,
);
int statusCode = response.statusCode;
String responseBody = response.body;
print(statusCode);
print(responseBody);
}
P.S I have checked the URL by posting from previous apk and it posts data within a second
Here is the Response Code
`Launching lib\main.dart on vivo 1906 in debug mode...
√ Built build\app\outputs\flutter-apk\app-debug.apk.
I/flutter (24141): Observatory listening on ************************************
I/flutter (24588): Observatory listening on ************************************
E/flutter (24588): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Converting object to an encodable object failed: Instance of 'DateTime'
E/flutter (24588): #0 _JsonStringifier.writeObject (dart:convert/json.dart:688:7)
E/flutter (24588): #1 _JsonStringifier.writeList (dart:convert/json.dart:736:7)
E/flutter (24588): #2 _JsonStringifier.writeJsonValue (dart:convert/json.dart:718:7)
E/flutter (24588): #3 _JsonStringifier.writeObject (dart:convert/json.dart:679:9)
E/flutter (24588): #4 _JsonStringStringifier.printOn (dart:convert/json.dart:877:17)
E/flutter (24588): #5 _JsonStringStringifier.stringify (dart:convert/json.dart:862:5)
E/flutter (24588): #6 JsonEncoder.convert (dart:convert/json.dart:262:30)
E/flutter (24588): #7 JsonCodec.encode (dart:convert/json.dart:172:45)
E/flutter (24588): #8 DataModel.makePostRequest
E/flutter (24588): #9 _FormState.build.<anonymous closure>.<anonymous closure>
E/flutter (24588): #10 _InkResponseState._handleTap
E/flutter (24588): #11 GestureRecognizer.invokeCallback
E/flutter (24588): #12 TapGestureRecognizer.handleTapUp
E/flutter (24588): #13 BaseTapGestureRecognizer._checkUp
E/flutter (24588): #14 BaseTapGestureRecognizer.acceptGesture
E/flutter (24588): #15 GestureArenaManager.sweep
E/flutter (24588): #16 GestureBinding.handleEvent
E/flutter (24588): #17 GestureBinding.dispatchEvent
E/flutter (24588): #18 RendererBinding.dispatchEvent
E/flutter (24588): #19 GestureBinding._handlePointerEventImmediately
E/flutter (24588): #20 GestureBinding.handlePointerEvent
E/flutter (24588): #21 GestureBinding._flushPointerEventQueue
E/flutter (24588): #22 GestureBinding._handlePointerDataPacket
E/flutter (24588): #23 _rootRunUnary (dart:async/zone.dart:1370:13)
E/flutter (24588): #24 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (24588): #25 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1170:7)
E/flutter (24588): #26 _invoke1 (dart:ui/hooks.dart:182:10)
E/flutter (24588): #27 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:282:7)
E/flutter (24588): #28 _dispatchPointerDataPacket (dart:ui/hooks.dart:96:31)
E/flutter (24588):
Exited (1)```