Why doesn’t the Autocomplete widget update the suggestions when rebuilt?

I’m trying to build a search bar where the user can look up for a place, and select a suggestion corresponding on what caracters they wrote.
I’m using BLOC, and an API to fetch the suggestions from the string the user wrote. The widget that I use to display the suggestion is ‘Autocomplete’.
My problem is that this Autocompletion widget doesn’t rebuild the suggestions, while the blocbuilder (parent of ‘Autocompletion’) is properly rebuilt.

My bloc to fetch the suggestions has 3 states: Error, PlaceSuggestionsLoading and PlaceSuggestionLoaded, and work fine.

This is what I’ve tried:

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class Place {
  final String name;
  Place({required this.name});
}

abstract class PlaceResearchState {}

class PlaceResearchInitial extends PlaceResearchState {}

class PlaceSuggestionsLoading extends PlaceResearchState {}

class PlaceSuggestionsLoaded extends PlaceResearchState {
  final List<Place> placeSuggestions;
  PlaceSuggestionsLoaded(this.placeSuggestions);
}

class PlaceSuggestionsError extends PlaceResearchState {
  final String message;
  PlaceSuggestionsError(this.message);
}

class PlaceResearchBloc extends Cubit<PlaceResearchState> {
  PlaceResearchBloc() : super(PlaceResearchInitial());

  void searchSuggestions(String query) {
    if (query.isEmpty) {
      emit(PlaceResearchInitial());
      return;
    }
    emit(PlaceSuggestionsLoading());
    Future.delayed(Duration(milliseconds: 500), () {
      final suggestions = _fetchPlaceSuggestions(query);
      if (suggestions.isNotEmpty) {
        emit(PlaceSuggestionsLoaded(suggestions));
      } else {
        emit(PlaceSuggestionsError("No suggestion found"));
      }
    });
  }

  List<Place> _fetchPlaceSuggestions(String query) {
    List<Place> allPlaces = [
      Place(name: 'New York'),
      Place(name: 'Paris'),
      Place(name: 'London'),
      Place(name: 'Hamburg'),
      Place(name: 'Barcelona')
    ];
    return allPlaces.where((place) => place.name.toLowerCase().contains(query.toLowerCase())).toList();
  }
}

class SearchBarWithBlocListener extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Autocomplete MRE")),
      body: Stack(
        children: [
          Positioned(
            top: 20,
            left: 10,
            right: 10,
            child: Material(
              elevation: 4,
              borderRadius: BorderRadius.circular(8),
              child: BlocProvider(
                create: (_) => PlaceResearchBloc(),
                child: BlocBuilder<PlaceResearchBloc, PlaceResearchState>(
                  builder: (context, state) {
                    List<String> suggestions = [];

                    if (state is PlaceSuggestionsLoaded) {
                      suggestions = state.placeSuggestions.map((place) => place.name).toList();
                    }

                    print("Bloc builder Suggestions :  $suggestions");

                    return Autocomplete<String>(
                      optionsBuilder: (TextEditingValue textEditingValue) {

                        print("Autocomplete suggestions :  $suggestions");

                        return suggestions.where(
                              (suggestion) => suggestion.toLowerCase().contains(textEditingValue.text.toLowerCase()),
                        );
                      },
                      displayStringForOption: (String option) => option,
                      onSelected: (String selection) {
                        print('Selected suggestion : $selection');
                      },
                      fieldViewBuilder: (BuildContext context, TextEditingController fieldTextEditingController, FocusNode fieldFocusNode, VoidCallback onFieldSubmitted) {
                        return TextField(
                          controller: fieldTextEditingController,
                          focusNode: fieldFocusNode,
                          onChanged: (value) {
                            // Search for suggestions on each modifications
                            context.read<PlaceResearchBloc>().searchSuggestions(value);
                          },
                          decoration: InputDecoration(
                            hintText: "Search for a place",
                            border: InputBorder.none,
                            contentPadding: EdgeInsets.symmetric(horizontal: 15, vertical: 10),
                          ),
                        );
                      },
                    );
                  },
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }
}

What I don’t undestand is that the first print:

print("Bloc builder Suggestions :  $suggestions");

is triggered when I get the new suggestions to show, but not the second one:

print("Autocomplete suggestions :  $suggestions");

and so the suggestions are not updated when they are loaded by the bloc.
I don’t get why the Autocomplete Widget is not rebuilt when the BlocBuilder is…
Am I doing something wrong, or not understanding something here ?

This is my pubspec.yaml:

# State management
flutter_bloc: ^8.1.6

New contributor

Eli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật