mohesu / map_location_picker

Map location picker for flutter Based on google_maps_flutter
Apache License 2.0
46 stars 69 forks source link

PlacesAutocomplete: A TextEditingController was used after being disposed. #10

Closed jansvanda closed 1 year ago

jansvanda commented 1 year ago

Hello, I love this library as it is exactly what i need. Sadly, it does not work as expect because no matter what i do, if there is any Widget tree update (I hope it is called like that), i get an error. For example if i have a tabview and on page one i got the PlacesAutocomplete widget, do nothing with it, and just switch to second page and then back, i get this red error widget saying "A TextEditingController was used after being disposed."

I have just tried the whole afternoon getting around it putting it in different views, but nothing really helps.

Steps to reproduce: This is the basic code i have now created to show the same error. With an Alertdialog. When i exit the dialog, and open again, it will show this error.

OH yeah and I never call dispose, that's why this si so weird!

I hope you you can point me to an solution! THANKS Jan

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:map_location_picker/map_location_picker.dart';

class UploadData extends StatefulWidget {
  @override
  _UploadData createState() => _UploadData();
}

class _UploadData extends State<UploadData> {

  TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(

        body: Container(
          child: Center(
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: TextField(
                onTap: () {
                  showDialog(
                      context: context,
                      builder: (context) {
                        return AlertDialog(
                          title: Text('Example'),
                          content: PlacesAutocomplete(
                            searchController: _controller,
                            apiKey: "MYAPIKEYYYYYY",
                            components: [Component(Component.country, "cz")],
                            searchHintText: "Zadejte kompletnรญ adresu",
                            mounted: mounted,
                            showBackButton: false,
                          ),
                          actions: <Widget>[
                            TextButton(
                              child: Text('back'),
                              onPressed: () {
                                setState(() {
                                });
                              },
                            ),
                            TextButton(
                              child: Text('next'),
                              onPressed: () {
                                setState(() {});
                              },
                            ),
                          ],
                        );
                      });
                },
              ),

            ),
          ),
    ));
  }
}
rvndsngwn commented 1 year ago

Thanks @jansvanda for using this package, I'll take a look at it. Stay tuned. ๐Ÿ™‚

rvndsngwn commented 1 year ago

Hey @jansvanda

You can now use it like this, I hope it works and is useful to you. Happy coding ๐Ÿ˜€

showDialog(
                context: context,
                builder: (context) {
                  return AlertDialog(
                    title: const Text('Example'),
                    content: PlacesAutocomplete(
                      apiKey: "YOUR_API_KEY_HERE",
                      searchHintText: "Search for a place",
                      mounted: mounted,
                      showBackButton: false,
                      onSuggestionSelected: (value) {
                        setState(() {
                          autocompletePlace =
                              value.structuredFormatting?.mainText ?? "";
                          initialValue = value;
                        });
                      },
                      onGetDetailsByPlaceId: (value) {
                        setState(() {
                          address = value?.result.formattedAddress ?? "";
                        });
                      },
                    ),
                    actions: <Widget>[
                      TextButton(
                        child: const Text('Done'),
                        onPressed: () => Navigator.of(context).pop(),
                      ),
                    ],
                  );
                },
              );
jansvanda commented 1 year ago

Wow just woke up and there is already a solution! Amazing gonna try it

jansvanda commented 1 year ago

The fix helped! Thanks! Is there any way it could scroll down the keyboard below the suggestions?

Or maybe ability to show the suggestions on top of the search bar? @rvndsngwn

Screenshot_20230125-122744.jpg Screenshot_20230125-122738.jpg

jansvanda commented 1 year ago

Hi @rvndsngwn I don't like to spam you, but can you please just help me with this last issue I have? The keyboard overlaps the suggestions, making it unusable if the Box is on the bottom of the page. TY

rvndsngwn commented 1 year ago

Hey @jansvanda, It'll take me a while to figure it out ๐Ÿ™‚

rvndsngwn commented 1 year ago

Hi @rvndsngwn I don't like to spam you, but can you please just help me with this last issue I have? The keyboard overlaps the suggestions, making it unusable if the Box is on the bottom of the page. TY

I'll be sure it's possible ๐Ÿ˜ƒ

rvndsngwn commented 1 year ago

In 1.1.0 more customizable options added, now you can customize the UI of the PlacesAutocomplete class acording to your need.

Or you can use the service AutoCompleteState, this a service of google auto-complete api.

jansvanda commented 1 year ago

@rvndsngwn thanks for the update. If i correctly understand, I have to use the suggestionsBoxVerticalOffset and set it to negative values to let the suggestions appear above the field? Or what was the solution you thought would be?

rvndsngwn commented 1 year ago

Yes maybe it's works, you can try your self. I haven't tried suggestionsBoxVerticalOffset. It's a part of UI and I'm provide all the things like: A basic working screen and services. You can make your own UI with the help of services ๐Ÿ™‚

rvndsngwn commented 1 year ago

If you want to help in your project you can Discord me at ArvindSangwan#4721 ๐Ÿ˜€

MajdSallora commented 1 day ago

same issue here

The following assertion was thrown while finalizing the widget tree:
A TextEditingController was used after being disposed.

Once you have called dispose() on a TextEditingController, it can no longer be used.
When the exception was thrown, this was the stack: 
#0      ChangeNotifier.debugAssertNotDisposed.<anonymous closure> (package:flutter/src/foundation/change_notifier.dart:183:9)
#1      ChangeNotifier.debugAssertNotDisposed (package:flutter/src/foundation/change_notifier.dart:190:6)
#2      ChangeNotifier.dispose (package:flutter/src/foundation/change_notifier.dart:379:27)
#3      _PlacesAutocompleteState.dispose (package:map_location_picker/src/autocomplete_view.dart:375:17)