As soon as I implement ExpansionTileControler it is not working

I´m trying to control a Expansiontile via a tilecontroler, but as soon as I integrate it, the app is broken when I klick the tile.

Sorry, I´m new to Flutter. Any hints?

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

class MyWidget extends StatefulWidget {
  const MyWidget({super.key});

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  int selectedTile = -1;

  final tileControllers = List.generate(5, (_) => ExpansionTileController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
        key: UniqueKey(),
        itemCount: 5,
        itemBuilder: (context, index) {
          return ExpansionTile(
            key: Key(index.toString()),
            controller: tileControllers[index],
            initiallyExpanded: index == selectedTile,
            title: Text('ExpansionTile $index'),
            subtitle: const Text('Trailing expansion arrow icon'),
            onExpansionChanged: ((newState) {
              if (newState) {
                setState(() {
                  selectedTile = index;
                });
              } else {
                setState(() {
                  selectedTile = -1;
                });
              }
            }),
            children: [
              ListTile(
                title: Text('This is tile number $index'),
              ),
            ],
          );
        },
      ),
    );
  }
}

Here is the error message:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building _SelectionKeepAlive(state:
_SelectionKeepAliveState#94f0c):
‘package:flutter/src/material/expansion_tile.dart’: Failed assertion: line 607 pos 12:
‘widget.controller?._state == null’: is not true.

Either the assertion indicates an error in the framework itself, or we should provide substantially
more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
https://github.com/flutter/flutter/issues/new?template=2_bug.yml

The relevant error-causing widget was:
ListView-[#5648c]
ListView:file:///Users/michaelroebbeling/development/Flutter-Files/SportApp-Flutter/lib/screens/EditTraining.dart:19:22

The above error is thrown because when onExpansionChanged is called, the state is already updating, so you cannot call setState(() {}) in onExpansionChanged. Also, since ExpansionTile controls its own state, you can do the following and it will expand/collapse the ExpansionTile widget.

class _MyWidgetState extends State<MyWidget> {
  int selectedTile = -1;
  final tileControllers = List.generate(5, (_) => ExpansionTileController());
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
        key: UniqueKey(),
        itemCount: 5,
        itemBuilder: (context, index) {
          return ExpansionTile(
            key: Key(index.toString()),
            controller: tileControllers[index],
            //initiallyExpanded: index == selectedTile,
            title: Text('ExpansionTile $index'),
            subtitle: const Text('Trailing expansion arrow icon'),
            onExpansionChanged: (bool newState) {
              if (newState) {
                selectedTile = index;
              } else {
                selectedTile = -1;
              }
            },
            children: [
              ListTile(
                title: Text('This is tile number $index'),
              ),
            ],
          );
        },
      ),
    );
  }
}

1

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