You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, when we use this request with http.post like this:
Future<SomeResponse> fetch(
SomeRequest data
) async {
Uri uri =Uri.parse("some url");
final response =await http.post(
uri,
body: data.toJson()
);
if (response.statusCode ==200){
SomeResponse resp =SomeResponse.fromJson(
jsonDecode(response.body)
);
return resp;
} else {
throwException('print this exception');
}
}
We get this error:
flutter: [!] Info: Error: type 'bool' is not a subtype of type 'String' in type cast
If we change the type of advanced from bool to String, this error disappears, but the server will reject such JSON because it validates that advanced should be boolean.
Follow the documentation in The JavaScript Object Notation (JSON) Data Interchange Format - RFC-7156 so any request with a body, will accept a JSON body that contains a boolean, following rule of JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).
Extra Information
flutter doctor -v:
[✓] Flutter (Channel stable, 3.19.1, on macOS 14.3.1 23D60 darwin-x64, locale en-US)
• Flutter version 3.19.1 on channel stable at /usr/local/Caskroom/flutter/3.16.3/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision abb292a07e (6 weeks ago), 2024-02-20 14:35:05 -0800
• Engine revision 04817c99c9
• Dart version 3.3.0
• DevTools version 2.31.1
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at /Users/agcar/Library/Android/sdk
• Platform android-34, build-tools 34.0.0
• ANDROID_HOME = /Users/agcar/Library/Android/sdk
• ANDROID_SDK_ROOT = /opt/homebrew/share/android-commandlinetools
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 15.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 15C500b
• CocoaPods version 1.15.2
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2023.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
[✓] IntelliJ IDEA Community Edition (version 2021.3.2)
• IntelliJ at /Applications/IntelliJ IDEA CE.app
• Flutter plugin version 68.1.3
• Dart plugin version 213.7371
[✓] VS Code (version 1.87.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.86.0
[✓] Connected device (3 available)
• A’s iPhone (mobile) • XXXXXXXXX-XXXXXXXXXX • ios • iOS 17.3.1 21D61
• macOS (desktop) • macos • darwin-x64 • macOS 14.3.1 XXXXX darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 123.0.6312.105
[✓] Network resources
• All expected network resources are available.
• No issues found!
Note: I'm hiding PII with X's
The text was updated successfully, but these errors were encountered:
Summary
When doing a POST rest call, if the JSON request contains a boolean value instead of a String value, then we get
Error: type 'bool' is not a subtype of type 'String' in type cast
. This goes against The JavaScript Object Notation (JSON) Data Interchange Format - RFC-7156.Description
This
SomeRequest
class:will create the following JSON (example):
However, when we use this request with
http.post
like this:We get this error:
If we change the type of
advanced
frombool
toString
, this error disappears, but the server will reject such JSON because it validates thatadvanced
should be boolean.If we read The JavaScript Object Notation (JSON) Data Interchange Format - RFC-7156, we could read that JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays)..
Acceptance Criteria
Follow the documentation in The JavaScript Object Notation (JSON) Data Interchange Format - RFC-7156 so any request with a body, will accept a JSON body that contains a boolean, following rule of JSON can represent four primitive types (strings, numbers, booleans, and null) and two structured types (objects and arrays).
Extra Information
flutter doctor -v
:Note: I'm hiding PII with X's
The text was updated successfully, but these errors were encountered: