Comments (5)
The issue is most definitely unrelated to hooks. The controller was correctly created, and the error isn't thrown by flutter_hooks but Flutter.
My guess is that you're trying to use the controller before it is associated with a widget, and therefore trigger this assert.
Also note that your controller isn't listened. So your UI won't update when the value changes.
Two things:
- Consider raising an issue on the Flutter repo to have them improve their error message
- Try wrapping your
leading
in a builder, such as HooKBuilder or ValueListenableBuilder.
This would delay theisExpanded
usage to a point where the controller is linked with a widgetleading: HookBuilder( builder: (context) { useListenable(expansionTileController); return expansionTileController.isExpanded ? Assets.icons.minus.svg() : Assets.icons.plus.svg() },
from flutter_hooks.
Firstly, thank you for the prompt response and your suggestions. I understand your point that the issue might be due to the ExpansionTileController being used before it's associated with a widget and the controller not being listened to.
Following your advice, I attempted to use HookBuilder along with useListenable for the ExpansionTileController. However, I've run into a type inference issue. When I use useListenable with ExpansionTileController, I encounter the following error:
Couldn't infer type parameter 'T'.
Tried to infer 'ExpansionTileController' for 'T' which doesn't work:
Type parameter 'T' is declared to extend 'Listenable?' producing 'Listenable?'.
The type 'ExpansionTileController' was inferred from:
Parameter 'listenable' declared as 'T' but argument is 'ExpansionTileController'.
Consider passing explicit type argument(s) to the generic.
This seems to suggest a type mismatch between ExpansionTileController and Listenable?, which useListenable expects.
Is there a workaround or a different approach you would recommend for ensuring that the ExpansionTileController is correctly used and listened to within the context of flutter_hooks? Any further guidance would be greatly appreciated. Thank you for your time and help.
from flutter_hooks.
My bad, it's looking like ExpansionTileController isn't a Listenable.
Looks like you have to use ExpansionTileController.of instead;
leading: Builder(
builder: (context) {
final controller = ExpansionTileController.of(context);
return expansionTileController.isExpanded
? Assets.icons.minus.svg()
: Assets.icons.plus.svg()
},
from flutter_hooks.
No worries. I tried do it your way, it does not throw the error but also not changing the icon
class CustomExpansionTile extends HookWidget {
final String title;
final List<Widget> children;
final EdgeInsets? tilePadding;
final bool showDivider;
const CustomExpansionTile({
required this.title,
required this.children,
this.tilePadding,
this.showDivider = true,
super.key,
});
@override
Widget build(BuildContext context) {
final expansionTileController = useExpansionTileController();
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Theme(
data: Theme.of(context).copyWith(dividerColor: Colors.transparent),
child: ExpansionTile(
controller: expansionTileController,
tilePadding: tilePadding,
expandedAlignment: Alignment.topLeft,
expandedCrossAxisAlignment: CrossAxisAlignment.start,
trailing: HookBuilder(
builder: (context) {
return expansionTileController.isExpanded
? Assets.icons.minus.svg()
: Assets.icons.plus.svg();
},
),
title: Text(
title,
style: context.appTextStyles.paragraph2SemiBold,
),
children: children,
),
),
if (showDivider) const CustomDivider(),
],
);
}
}
I will stick to useState instead.
@override
Widget build(BuildContext context) {
final isExpanded = useState(false);
return Column(
mainAxisSize: MainAxisSize.min,
children: [
Theme(
data: Theme.of(context).copyWith(dividerColor: Colors.transparent),
child: ExpansionTile(
tilePadding: tilePadding,
expandedAlignment: Alignment.topLeft,
expandedCrossAxisAlignment: CrossAxisAlignment.start,
trailing: isExpanded.value
? Assets.icons.minus.svg()
: Assets.icons.plus.svg(),
title: Text(
title,
style: context.appTextStyles.paragraph2SemiBold,
),
onExpansionChanged: (bool expanded) => isExpanded.value = expanded,
children: children,
),
),
if (showDivider) const CustomDivider(),
],
);
}
from flutter_hooks.
It's ultimately still unrelated to flutter_hooks. So I'd redirect you to Reddit/StackOverflow/Discord for further help requests :)
from flutter_hooks.
Related Issues (20)
- [BUG] shouldPreserveState may wrongfully preserve state when it shouldn't HOT 1
- Feature Request: `useListener` hook HOT 3
- about the useMemoized API description
- Add useSound Hook HOT 2
- flutter_hooks: ^0.20.3 HOT 2
- Add hook to observe `didChangeDependencies`
- Why list not updating? HOT 7
- EditText reverting to previous value HOT 2
- Implement useTabControllerList for List<TabController> HOT 6
- Clarify usage of useFuture through documentation HOT 1
- Inconsistency in AppLifecycleState Detection Between iOS and Android in Flutter Hooks HOT 5
- Possibility of deprecating useIsMounted HOT 2
- `when` for `AsyncSnapshot` HOT 1
- a hook for the DraggableScrollableController HOT 2
- Using useFuture and useStream keeps invoking the widget build method unnecessarily. HOT 5
- Setting TextEditingController.text selects input in TextField in Web HOT 1
- onAttach and onDetach is missing on usePageController
- ERROR: Undefined class 'WidgetStatesController'. HOT 4
- [Feature Request] Is it possible to implement `useSWR` in flutter_hooks HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutter_hooks.