This is the Exception I am getting in the debug Console
Restarted application in 2,400ms.
Reloaded 0 libraries in 2,029ms.
E/MethodChannel#flutter/textinput(24087): Failed to handle method call
E/MethodChannel#flutter/textinput(24087): java.lang.IndexOutOfBoundsException: invalid selection start: 2
E/MethodChannel#flutter/textinput(24087): at io.flutter.embedding.engine.systemchannels.TextInputChannel$TextEditState.<init>(TextInputChannel.java:724)
E/MethodChannel#flutter/textinput(24087): at io.flutter.embedding.engine.systemchannels.TextInputChannel$TextEditState.fromJson(TextInputChannel.java:680)
E/MethodChannel#flutter/textinput(24087): at io.flutter.embedding.engine.systemchannels.TextInputChannel$1.onMethodCall(TextInputChannel.java:91)
E/MethodChannel#flutter/textinput(24087): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/MethodChannel#flutter/textinput(24087): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/MethodChannel#flutter/textinput(24087): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818)
E/MethodChannel#flutter/textinput(24087): at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#flutter/textinput(24087): at android.os.MessageQueue.next(MessageQueue.java:336)
E/MethodChannel#flutter/textinput(24087): at android.os.Looper.loop(Looper.java:174)
E/MethodChannel#flutter/textinput(24087): at android.app.ActivityThread.main(ActivityThread.java:7697)
E/MethodChannel#flutter/textinput(24087): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#flutter/textinput(24087): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
E/MethodChannel#flutter/textinput(24087): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
E/flutter (24087): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(error, invalid selection start: 2, null, java.lang.IndexOutOfBoundsException: invalid selection start: 2
E/flutter (24087): at io.flutter.embedding.engine.systemchannels.TextInputChannel$TextEditState.<init>(TextInputChannel.java:724)
E/flutter (24087): at io.flutter.embedding.engine.systemchannels.TextInputChannel$TextEditState.fromJson(TextInputChannel.java:680)
E/flutter (24087): at io.flutter.embedding.engine.systemchannels.TextInputChannel$1.onMethodCall(TextInputChannel.java:91)
E/flutter (24087): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:233)
E/flutter (24087): at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:85)
E/flutter (24087): at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:818)
E/flutter (24087): at android.os.MessageQueue.nativePollOnce(Native Method)
E/flutter (24087): at android.os.MessageQueue.next(MessageQueue.java:336)
E/flutter (24087): at android.os.Looper.loop(Looper.java:174)
E/flutter (24087): at android.app.ActivityThread.main(ActivityThread.java:7697)
E/flutter (24087): at java.lang.reflect.Method.invoke(Native Method)
E/flutter (24087): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
E/flutter (24087): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
E/flutter (24087): )
[38;5;244mE/flutter (24087): #0 JSONMethodCodec.decodeEnvelope[39;49m
[38;5;244mE/flutter (24087): #1 MethodChannel._invokeMethod[39;49m
E/flutter (24087): <asynchronous suspension>
E/flutter (24087):
What I have tried:
I know the reason for this exception but I am not able to identify where is the bug present in the code. I get the exception whenever I click on the icon button to clear the text field. This is my code:
import 'package:flutter/material.dart';
import 'package:geoflutterfire/geoflutterfire.dart';
import 'package:geolocator/geolocator.dart';
import 'package:maps_app/models/place.dart';
import 'package:maps_app/services/location_api.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
import 'package:app_settings/app_settings.dart';
class SearchView extends StatefulWidget {
@override
_SearchViewState createState() => _SearchViewState();
}
class _SearchViewState extends State<SearchView> {
final _scrollController = ScrollController();
var latitude, longitude;
var con_width = 0.0;
var list_height = 0.0;
bool locEnabled = false;
GeoFirePoint point;
Geoflutterfire geo = Geoflutterfire();
bool serviceEnabled = false;
LocationPermission permission;
bool per;
@override
void initState() {
super.initState();
}
Future getCurrentLocation() async {
try {
final pos = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
setState(() {
latitude = pos.latitude;
longitude = pos.longitude;
});
} catch (e) {
print(e);
}
}
Future<bool> checkService() async {
serviceEnabled = await Geolocator.isLocationServiceEnabled();
return serviceEnabled;
}
Future checkPermission() async {
permission = await Geolocator.checkPermission();
per = await Permission.location.isPermanentlyDenied;
print(permission);
print(per.toString());
return permission;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SearchInjector(
child: SafeArea(
child: Consumer<LocationApi>(
builder: (_, api, child) => SingleChildScrollView(
child: Container(
padding: const EdgeInsets.all(16),
child: Column(
children: [
Container(
child: TextField(
onTap: () {
setState(() {
list_height = 56;
});
},
controller: api.addressController,
decoration: InputDecoration(
prefixIcon: Icon(Icons.my_location),
suffixIcon: IconButton(
icon: Icon(Icons.clear),
onPressed: () {
setState(() {
api.addressController.clear();
con_width = 0.0;
list_height = 0.0;
api.dispose();
});
}),
border: OutlineInputBorder(
borderSide: BorderSide(
color: Colors.red,
),
borderRadius: BorderRadius.circular(50.0),
),
labelText: 'Enter Your Address',
fillColor: Colors.grey[20],
filled: true),
onChanged: (value) {
setState(() {
con_width = 300.0;
});
api.places.clear();
api.handleSearch(value);
},
),
),
AnimatedContainer(
duration: Duration(milliseconds: 20),
color: Colors.blue[100].withOpacity(.3),
width: MediaQuery.of(context).size.width,
height: con_width,
child: StreamBuilder<List<Place>>(
stream: api.controllerOut,
builder: (context, snapshot) {
if (snapshot.data == null) {
return Center(
child: Text('No data address found'));
}
final data = snapshot.data;
return Scrollbar(
controller: _scrollController,
child: SingleChildScrollView(
controller: _scrollController,
child: Container(
child: Builder(builder: (context) {
return Column(
children: [
Column(
children: List.generate(data.length,
(index) {
final place = data[index];
return ListTile(
leading: Icon(Icons.home),
onTap: () {
point = geo.point(
latitude: place.latitude,
longitude: place.longitude);
setState(() {
api.addressController.text =
'${place.name}, ${place.subLocality}, ${place.country}, ${place.postalCode}';
con_width = 0.0;
list_height = 0.0;
});
},
title: Text(
'${place.name}, ${place.subLocality}, ${place.locality}, ${place.postalCode} '),
subtitle: Text(
'${place.adminArea}, ${place.country}'),
);
})),
],
);
}),
),
),
);
}),
)
],
),
),
),
),
),
),
);
}
}
class SearchInjector extends StatelessWidget {
final Widget child;
const SearchInjector({Key key, this.child}) : super(key: key);
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider(
create: (_) => LocationApi(),
child: child,
);
}
}