我刚刚开始使用material-ui学习reactjs,但是在将样式应用于我的组件时遇到此错误。它是我的代码:
const useStyles = makeStyles(theme => ({
root: {
flexGrow: 1,
},
menuButton: {
marginRight: theme.spacing(2),
},
title: {
flexGrow: 1,
},
}));
class NavMenu extends React.Component {
constructor(props) {
super(props);
this.state = {
isOpen: false
};
}
render() {
const classes = useStyles();
return (
<div className={classes.root}>
<AppBar position="static">
<Toolbar>
<IconButton edge="start" className={classes.menuButton} color="inherit" aria-label="Menu">
<MenuIcon />
</IconButton>
<Typography variant="h6" className={classes.title}>
News
</Typography>
<Button color="inherit">Login</Button>
</Toolbar>
</AppBar>
</div>
);
}
}
export default NavMenu;
Run Code Online (Sandbox Code Playgroud)
这是错误:

我需要根据当前应用程序区域设置使用提供程序在 initState 方法中获取数据。
这是我的代码:
initState() {
super.initState();
Locale myLocale = Localizations.localeOf(context);
context.read<ApiProvider>().getHomeData(myLocale.languageCode);
}
Run Code Online (Sandbox Code Playgroud)
导致此错误:
在 _HomePageState.initState() 完成之前调用 dependentOnInheritedWidgetOfExactType<_LocalizationsScope>() 或 dependentOnInheritedElement()。