Pannam T
Pannam T

Reputation: 479

How do I setup Provider correctly?

I have 3 classes where I want to pass the data, The first class is the receiver, 2nd class is the changenotifier and the 3rd is the sender, I did set up the notifer but can't pass data from the third class to the first

First class contains a textfield where i want the text

Text(
    (Provider.of < AppData > (context)
        .dropOffLocation !=
        null ?
        Provider.of < AppData > (context)
        .dropOffLocation :
        "Search Drop off"),
    style: TextStyle(fontSize: 12.0),
    overflow: TextOverflow.ellipsis,
),

The ChangeNotifier class

class AppData extends ChangeNotifier {
  String dropOffLocation;

  void dropOffLocationAddress(String dropOffLoc) {
    dropOffLocation = dropOffLoc;
    notifyListeners();
  }
}

Lastly the class from where I want to send the data but I get an error The expression doesn't evaluate to a function

class _SearchScreenState extends State<SearchScreen> {
  TextEditingController pickUpTextEditingController = TextEditingController();
  TextEditingController dropOffTextEditingController = TextEditingController();
  final AppData dropoffText = new AppData();

// I want to send the text in dropOffTextEditingController from below to the first class.
child: TypeAheadField(
   onSuggestionSelected: (suggestion) async {
    dropOffTextEditingController.text =
        (suggestion as SearchInfo).address.name;
 Provider.of<AppData>(context, listen: false)
          .dropOffLocation(dropOffTextEditingController.text);

},
)
....

Upvotes: 1

Views: 93

Answers (1)

zerochill
zerochill

Reputation: 746

Maybe because you are not calling the function correctly, try this;

Provider.of<AppData>(context, listen: false)
          .dropOffLocationAddress(dropOffTextEditingController.text);

Upvotes: 1

Related Questions