title: Why does my setState method not rebuild the UI of my Chat dialog (Flutter)
body: <p>I have a dialog Widget that renders a ChatUI which takes a List as an argument for the messages to display. Whenever I call the onSendPressed function of the Chat Widget, I want to send the message the user wrote, add this message to my _messages list, update the Chat Widget AND THEN based on what the user wrote calculate a response message, add that to the _messages list and update the Chat Widget again (so that the UI updates two times in total).</p>
  <p>For some reason I haven't figured out yet my implementation does not work as intended, maybe one of you guys knows why? This is my code below:</p>
  <pre><code>
  
  String randomString() {
    final random = Random.secure();
    final values = List&lt;int&gt;.generate(16, (i) =&gt; random.nextInt(255));
    return base64UrlEncode(values);
  }
  
  class ChatDialog extends StatefulWidget {
    const ChatDialog({Key? key}) : super(key: key);
  
    @override
    _ChatDialogState createState() =&gt; _ChatDialogState();
  }
  
  class _ChatDialogState extends State&lt;ChatDialog&gt; {
    late DialogflowApi _dialog;
    @override
    void initState() {
      super.initState();
    }
  
    Future _detectIntent(String text) async {
      // calculates the respective answer...
  
      return answer;
    }
  
    final List&lt;types.Message&gt; _messages = [];
    final _user = const types.User(id: '06c33e8b-e835-4736-80f4-63f44b66666c');
    final _user2 = const types.User(id: '03c33e8b-e835-4736-80f4-63f44b66666c');
  
    void _addMessage(types.Message message) {
      setState(() {
        _messages.insert(0, message);
      });
    }
  
    void _handleSendPressed(types.PartialText message) async {
      final textMessage = types.TextMessage(
        author: _user,
        createdAt: DateTime.now().millisecondsSinceEpoch,
        id: randomString(),
        text: message.text,
      );
  
      _addMessage(textMessage);
  
      print('added first message');
  
      final String answer = await _detectIntent(message.text) as String;
  
      final textMessage2 = types.TextMessage(
        author: _user2,
        createdAt: DateTime.now().millisecondsSinceEpoch,
        id: randomString(),
        text: answer,
      );
  
      _addMessage(textMessage2);
    }
  
    @override
    Widget build(BuildContext context) {
      return Stack(
        children: [
          Dialog(
            backgroundColor: backgroundLight,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(25.0),
            ),
            insetPadding: EdgeInsets.only(
              top: 500,
              left: MediaQuery.of(context).size.width * 0.65,
            ),
            child: Padding(
              padding: const EdgeInsets.all(10.0),
              child: SizedBox(
                height: 400,
                width: 200,
                child: Chat(
                  theme: DefaultChatTheme(
                    inputBorderRadius: const BorderRadius.all(
                      Radius.circular(25.0),
                    ),
                    primaryColor: Theme.of(context).primaryColor,
                  ),
                  sendButtonVisibilityMode: SendButtonVisibilityMode.always,
                  emptyState: const Center(
                    child: PTText(text: 'How can I help you'),
                  ),
                  messages: _messages,
                  onSendPressed: _handleSendPressed,
                  user: _user,
                ),
              ),
            ),
          )
        ],
      );
    }
  }
  
  
  </code></pre>
  <p>You can ignore the print statements and the static ID's etc, just makes it easier for the moment to test things until they work.</p>
  <p>What I want to happen is that after every of the two _addMessage calls, the UI is rebuild, what happens currently is that the UI os only rebuild once at the end of _handleSendPressed after calling _detectIntent</p>