I’m new to flutter and I want to implement the useEffect
hook.
Here is my widget:
import 'dart:developer';
import 'package:flutter/material.dart';
class MarketRunnerChart extends StatefulWidget {
const MarketRunnerChart({Key? key}) : super(key: key);
@override
State<MarketRunnerChart> createState() => _MarketRunnerChartState();
}
class _MarketRunnerChartState extends State<MarketRunnerChart> {
@override
Widget build(BuildContext context) {
useEffect(() {
log('okok');
}, []);
return Text("Some text");
}
}
But I got the error The method 'useEffect' isn't defined for the type '_MarketRunnerChartState'.
When I remove the useEffect
hook out of the build
function and put it directly in the class I got error 'useEffect' must have a method body because '_MarketRunnerChartState' isn't abstract.
I’m used to work with React, but right now with flutter I can’t figure out how to implement that hook.
How am I supposed to do this ?
3
Answers
try add
import 'package:flutter_hooks/flutter_hooks.dart';
on top of your class file
import flutter hooks
You can follow the doc example, import
flutter_hooks
, extend theHookWidget
.More about useEffect