I used Freezed package and need generating json . But when get response from API faced error type 'String' is not a subtype of type 'int' in type cast. This error known because my response from API String but i make integer in model
After searching to fixed solution , i found to solved this from this issue .But when build the application i get this error :
Launching lib\main.dart on Redmi Note 4 in debug mode...
Compiler message:
lib/src/network/models/utang/utang_model.freezed.dart:223:20: Error: Getter not found: '_stringToInt'.
#JsonKey(fromJson: _stringToInt, toJson: _stringFromInt)
^^^^^^^^^^^^
lib/src/network/models/utang/utang_model.freezed.dart:223:42: Error: Getter not found: '_stringFromInt'.
#JsonKey(fromJson: _stringToInt, toJson: _stringFromInt)
^^^^^^^^^^^^^^
Compiler message:
lib/src/network/models/utang/utang_model.freezed.dart:223:20: Error: Getter not found: '_stringToInt'.
#JsonKey(fromJson: _stringToInt, toJson: _stringFromInt)
^^^^^^^^^^^^
lib/src/network/models/utang/utang_model.freezed.dart:223:42: Error: Getter not found: '_stringFromInt'.
#JsonKey(fromJson: _stringToInt, toJson: _stringFromInt)
^^^^^^^^^^^^^^
Target kernel_snapshot failed: Exception: Errors during snapshot creation: null
build failed.
FAILURE: Build failed with an exception.
* Where:
Script 'C:\flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 882
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 23s
Exception: Gradle task assembleDebug failed with exit code 1
Exited (sigterm)
Model
#freezed
abstract class UtangModel with _$UtangModel {
#JsonSerializable(fieldRename: FieldRename.snake)
#JsonKey(fromJson: _stringToInt, toJson: _stringFromInt)
const factory UtangModel({
String idUtang,
UserGoogleModel pembertang,
UserGoogleModel pengutang,
int totalUtang,
int sisaUtang,
DateTime tglKembali,
String status,
String keterangan,
String selfie,
String ttd,
DateTime createdDate,
}) = _UtangModel;
factory UtangModel.fromJson(Map<String, dynamic> json) => _$UtangModelFromJson(json);
static int _stringToInt(String number) => number == null ? null : int.parse(number);
static String _stringFromInt(int number) => number?.toString();
}
I missed something ?
You have to indicate that this function is from the abstract class UtangModel:
#freezed
abstract class UtangModel with _$UtangModel {
#JsonSerializable(fieldRename: FieldRename.snake)
const factory UtangModel({
String idUtang,
UserGoogleModel pembertang,
UserGoogleModel pengutang,
#JsonKey(fromJson: UtangModel._stringToInt, toJson: UtangModel._stringFromInt)int totalUtang,
int sisaUtang,
DateTime tglKembali,
String status,
String keterangan,
String selfie,
String ttd,
DateTime createdDate,
}) = _UtangModel;
factory UtangModel.fromJson(Map<String, dynamic> json) => _$UtangModelFromJson(json);
static int _stringToInt(String number) => number == null ? null : int.parse(number);
static String _stringFromInt(int number) => number?.toString();
}
Related
/C:/src/flutter/.pub-cache/hosted/pub.dev/modal_bottom_sheet-2.1.2/lib/src/material_with_modal_page_route.dart:4:1: Error: 'ModalBottomSheetRoute' is imported from both 'package:flutter/src/material/bottom_sheet.dart' and 'package:modal_bottom_sheet/src/bottom_sheet_route.dart'.
import '../modal_bottom_sheet.dart';
^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dev/modal_bottom_sheet-2.1.2/lib/src/bottom_sheets/material_bottom_sheet.dart:28:13: Error: 'ModalBottomSheetRoute' is imported from both 'package:flutter/src/material/bottom_sheet.dart' and 'package:modal_bottom_sheet/src/bottom_sheet_route.dart'.
.push(ModalBottomSheetRoute<T>(
^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dev/modal_bottom_sheet-2.1.2/lib/src/bottom_sheets/material_bottom_sheet.dart:50:10: Error: A value of type 'Object?' can't be returned from an async function with return type 'Future<T?>'.
- 'Object' is from 'dart:core'.
- 'Future' is from 'dart:async'.
return result;
^
/C:/src/flutter/.pub-cache/hosted/pub.dev/modal_bottom_sheet-2.1.2/lib/src/bottom_sheets/bar_bottom_sheet.dart:102:13: Error: 'ModalBottomSheetRoute' is imported from both 'package:flutter/src/material/bottom_sheet.dart' and 'package:modal_bottom_sheet/src/bottom_sheet_route.dart'.
.push(ModalBottomSheetRoute<T>(
^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/.pub-cache/hosted/pub.dev/modal_bottom_sheet-2.1.2/lib/src/bottom_sheets/bar_bottom_sheet.dart:125:10: Error: A value of type 'Object?' can't be returned from an async function with return type 'Future<T?>'.
- 'Object' is from 'dart:core'.
- 'Future' is from 'dart:async'.
return result;
^
Target kernel_snapshot failed: Exception
FAILURE: Build failed with an exception.
* Where:
Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1151
* What went wrong:
Execution failed for task ':app:compileFlutterBuildRelease'.
> Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 35s
Running Gradle task 'assembleRelease'... 36.7s
Gradle task assembleRelease failed with exit code 1
solution for this error
/E:/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore_platform_interface-5.5.0/lib/src/method_channel/method_channel_firestore.dart:112:15: Error: Method not found: 'Error.throwWithStackTrace'.
Error.throwWithStackTrace(
^^^^^^^^^^^^^^^^^^^
/E:/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore_platform_interface-5.5.0/lib/src/method_channel/utils/exception.dart:14:11: Error: Method not found: 'Error.throwWithStackTrace'.
Error.throwWithStackTrace(exception, stackTrace);
^^^^^^^^^^^^^^^^^^^
/E:/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore_platform_interface-5.5.0/lib/src/method_channel/utils/exception.dart:17:9: Error: Method not found: 'Error.throwWithStackTrace'.
Error.throwWithStackTrace(
^^^^^^^^^^^^^^^^^^^
/E:/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore_platform_interface-5.5.0/lib/src/method_channel/utils/exception.dart:12:7: Error: A non-null value must be returned since the return type 'Never' doesn't allow null.
Never convertPlatformException(Object exception, StackTrace stackTrace) {
^
FAILURE: Build failed with an exception.
Where:
Script 'E:\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1035
What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
Process 'command 'E:\flutter\bin\flutter.bat'' finished with non-zero exit value 1
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
BUILD FAILED in 36s
Exception: Gradle task assembleDebug failed with exit code 1
getData() async {
CollectionReference userRef = FirebaseFirestore.instance.collection("user");
QuerySnapshot querySnapshot = await userRef.get();
List<QueryDocumentSnapshot> listDocs = querySnapshot.docs;
listDocs.forEach((element) {
print(element);
});
}
I try to implement an app. And I run it several times with no errors. But without any reason, and after a day I try to run my app, I got the below error, and I try to find a solution for id but I did not find anything.
So can someone provide me how can I solve it?
Launching lib\main.dart on sdk gphone x86 in debug mode...
lib\main.dart
../../../flutter/flutter/.pub-cache/hosted/pub.dartlang.org/retry-3.0.1/lib/retry.dart:131:16: Error: Expected ';' after this.
return Pestawait fn();
^^^^^^^^^
../../../flutter/flutter/.pub-cache/hosted/pub.dartlang.org/retry-3.0.1/lib/retry.dart:131:16: Error: The getter 'Pestawait' isn't defined for the class 'RetryOptions'.
- 'RetryOptions' is from 'package:retry/retry.dart' ('../../../flutter/flutter/.pub-cache/hosted/pub.dartlang.org/retry-3.0.1/lib/retry.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'Pestawait'.
return Pestawait fn();
^^^^^^^^^
../../../flutter/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_datetime_picker-1.4.0/lib/flutter_datetime_picker.dart:215:48: Error: No named parameter with the name 'shadowThemeOnly'.
ThemeData inheritTheme = Theme.of(context, shadowThemeOnly: true);
^^^^^^^^^^^^^^^
../../../flutter/flutter/packages/flutter/lib/src/material/theme.dart:119:20: Context: Found this candidate, but the arguments don't match.
static ThemeData of(BuildContext context) {
^^
FAILURE: Build failed with an exception.
* Where:
Script 'D:\mohy\flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 900
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'D:\mohy\flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 9m 1s
Exception: Gradle task assembleDebug failed with exit code 1
Exited (sigterm)
It seems your library is broken. You can delete that folder and install that package again. Or for a quick fix, you can change that part in the related file as in here.
Future<T> retry<T>(
FutureOr<T> Function() fn, {
FutureOr<bool> Function(Exception)? retryIf,
FutureOr<void> Function(Exception)? onRetry,
}) async {
var attempt = 0;
// ignore: literal_only_boolean_expressions
while (true) {
attempt++; // first invocation is the first attempt
try {
return await fn(); // Here is broken!
} on Exception catch (e) {
if (attempt >= maxAttempts ||
(retryIf != null && !(await retryIf(e)))) {
rethrow;
}
if (onRetry != null) {
await onRetry(e);
}
}
// Sleep for a delay
await Future.delayed(delay(attempt));
}
}
}
I am developing a flutter app.My flutter project shows error in factory constructor when I use DocumentSnapshot
User.dart
import 'package:cloud_firestore/cloud_firestore.dart';
class User {
final String id;
final String username;
final String email;
final String photoUrl;
final String displayname;
final String bio;
User({
this.id,
this.username,
this.email,
this.photoUrl,
this.displayname,
this.bio
});
factory User.fromDoc(DocumentSnapshot doc){
return User(
id: doc['id'],
username: doc['username'],
email: doc['email'],
photoUrl: doc['photoUrl'],
displayname: doc['displayName'],
bio: doc['bio'],
);
}
}
Error log
Launching lib\main.dart on sdk gphone x86 arm in debug mode...
Running Gradle task 'assembleDebug'...
lib/models/user.dart:22:14: Error: The operator '[]' isn't defined for the class 'DocumentSnapshot'.
- 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.14.0+2/lib/cloud_firestore.dart').
Try correcting the operator to an existing operator, or defining a '[]' operator.
id: doc['id'],
^
lib/models/user.dart:23:20: Error: The operator '[]' isn't defined for the class 'DocumentSnapshot'.
- 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.14.0+2/lib/cloud_firestore.dart').
Try correcting the operator to an existing operator, or defining a '[]' operator.
username: doc['username'],
^
lib/models/user.dart:24:17: Error: The operator '[]' isn't defined for the class 'DocumentSnapshot'.
- 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.14.0+2/lib/cloud_firestore.dart').
Try correcting the operator to an existing operator, or defining a '[]' operator.
email: doc['email'],
^
lib/models/user.dart:25:20: Error: The operator '[]' isn't defined for the class 'DocumentSnapshot'.
- 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.14.0+2/lib/cloud_firestore.dart').
Try correcting the operator to an existing operator, or defining a '[]' operator.
photoUrl: doc['photoUrl'],
^
lib/models/user.dart:26:23: Error: The operator '[]' isn't defined for the class 'DocumentSnapshot'.
- 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.14.0+2/lib/cloud_firestore.dart').
Try correcting the operator to an existing operator, or defining a '[]' operator.
displayname: doc['displayName'],
^
lib/models/user.dart:27:15: Error: The operator '[]' isn't defined for the class 'DocumentSnapshot'.
- 'DocumentSnapshot' is from 'package:cloud_firestore/cloud_firestore.dart' ('/C:/flutter/flutter/.pub-cache/hosted/pub.dartlang.org/cloud_firestore-0.14.0+2/lib/cloud_firestore.dart').
Try correcting the operator to an existing operator, or defining a '[]' operator.
bio: doc['bio'],
^
FAILURE: Build failed with an exception.
* Where:
Script 'C:\flutter\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 896
* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\flutter\flutter\bin\flutter.bat'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 7s
Exception: Gradle task assembleDebug failed with exit code 1
Is there any way to correct this operator problem because I have to use DocumentSnapshot in various sections of my app? If there any permanent solution available to use this kind of methods?
Thank You
If you want to access the firebases DocumentSnapshot you need to access its data attribute snapshot.data. If you want to access the map behind it use snapshot.data.data
So in your example something like:
factory User.fromDoc(DocumentSnapshot doc){
return User(
id: doc.data.data['id'],
username: doc.data.data['username'],
email: doc.data.data['email'],
photoUrl: doc.data.data['photoUrl'],
displayname: doc.data.data['displayName'],
bio: doc.data.data['bio'],
);
}
}
See the One-Time Read section in the official documentation:
https://firebase.flutter.dev/docs/firestore/usage/
I am tried to use flutter_stetho package when it Compile the compiler shown ..
Launching lib/main.dart on Android SDK built for x86 in debug mode...
Compiler message:
file:///home/administrator/softwares/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_stetho-0.2.2/lib/src/http_client_response.dart:4:7: Error: The non-abstract class 'StethoHttpClientResponse' is missing implementations for these members:
- HttpClientResponse.compressionState
Try to either
- provide an implementation,
- inherit an implementation from a superclass or mixin,
- mark the class as abstract, or
- provide a 'noSuchMethod' implementation.
class StethoHttpClientResponse extends StreamView<List<int>>
^^^^^^^^^^^^^^^^^^^^^^^^
org-dartlang-sdk:///third_party/dart/sdk/lib/_http/http.dart:1967:42: Context: 'HttpClientResponse.compressionState' is defined here.
HttpClientResponseCompressionState get compressionState;
^^^^^^^^^^^^^^^^
Compiler failed on /home/administrator/projects/mobile/sampleapp/lib/main.dart
FAILURE: Build failed with an exception.
* Where:
Script '/home/administrator/softwares/flutter/packages/flutter_tools/gradle/flutter.gradle' line: 665
* What went wrong:
Execution failed for task ':app:compileflutterBuildDebugX86'.
> Process 'command '/home/administrator/softwares/flutter/bin/flutter'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 20s
Gradle task assembleDebug failed with exit code 1
Exited (sigterm)
I have just faced the same error, I'm leaving the stack trace below for reference.
For me it was caused because I left a reference to a parameter in a copy-pasted method.
I had this:
Map<String, dynamic> toJson() {
return User(
login: json['login'],
password: json['password'],
firstName: json['firstName'],
lastName: json['lastName'],
email: json['email'],
cellphone: json['cellphone'],
);
}
But the correct code is actually:
Map<String, dynamic> toJson() {
return {
'login': login,
'password': password,
'firstName': firstName,
'lastName': lastName,
'email': email,
'cellphone': cellphone,
};
}
What was odd or sneaky in my case was that Visual Studio Code was not pointing out the error after opening the file.
Also, I left the constructor method from the class I based on (copy pasted) which was User
Stack trace:
Compiler message:
lib/models/rest/user_post.dart:3:7: Error: The non-abstract class 'UserPost' is missing implementations for these members:
- UserPost.User
Try to either
- provide an implementation,
- inherit an implementation from a superclass or mixin,
- mark the class as abstract, or
- provide a 'noSuchMethod' implementation.
class UserPost {
^^^^^^^^
lib/models/rest/user_post.dart:33:3: Context: 'UserPost.User' is defined here.
User({
^^^^
lib/models/rest/user_post.dart:15:14: Error: Getter not found: 'json'.
login: json['login'],
^^^^
lib/models/rest/user_post.dart:16:17: Error: Getter not found: 'json'.
password: json['password'],
^^^^
lib/models/rest/user_post.dart:17:18: Error: Getter not found: 'json'.
firstName: json['firstName'],
^^^^
lib/models/rest/user_post.dart:18:17: Error: Getter not found: 'json'.
lastName: json['lastName'],
^^^^
lib/models/rest/user_post.dart:19:14: Error: Getter not found: 'json'.
email: json['email'],
^^^^
lib/models/rest/user_post.dart:20:18: Error: Getter not found: 'json'.
cellphone: json['cellphone'],
^^^^