I am using below code to pick the image from the gallery in flutter application.
Upon picking the application I want to update the int variables
i.e. a
and total
as shown in the code, but when executing the same I facing the error as shown below
Method
Future getImage1() async {
print("get image");
PickedFile image = await _picker1.getImage(source: ImageSource.gallery);
if (image != null) {
setState(() {
final File file = File(image.path);
avatarImageFile1 = file;
isLoading1 = true;
a=1;
total = total +a;
print(total);
});
}
}
ERROR LOGS
[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method '+' was called on null. Receiver: null Tried calling: +(1) #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5) #1 _AddImageState.getImage1. (package:nextproject/addandeditrecipe/add_image.dart:70:25) #2 State.setState (package:flutter/src/widgets/framework.dart:1233:30) #3 _AddImageState.getImage1 (package:nextproject/addandeditrecipe/add_image.dart:65:7) #4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:779:19) #5 _InkResponseState.build. (package:flutter/src/material/ink_well.dart:862:36) #6 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24) #7 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:504:11) #8 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:282:5) #9 BaseTapGestureRecog<…>
NoSuchMethodError: The method '+' was called on null.
This is likely because of the "total" variable. Have you declared it somewhere? Try initializing it first. If it is already initialized it may not be in the scope of the function.