'I imported search bar in flutter, and I got some error messages
Code: /// Flutter code sample for BottomNavigationBar
// This example shows a [BottomNavigationBar] as it is used within a [Scaffold]
// widget. The [BottomNavigationBar] has three [BottomNavigationBarItem]
// widgets, which means it defaults to [BottomNavigationBarType.fixed], and
// the [currentIndex] is set to index 0. The selected item is
// amber. The `_onItemTapped` function changes the selected item's index
// and displays a corresponding message in the center of the [Scaffold].
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_search_bar/flutter_search_bar.dart';
import 'package:flutter_login/flutter_login.dart';
const users = const {
'[email protected]': '12345',
'[email protected]': 'hunter',
};
class LoginScreen extends StatelessWidget {
Duration get loginTime => Duration(milliseconds: 2250);
Future<String> _authUser(LoginData data) {
print('Name: ${data.name}, Password: ${data.password}');
return Future.delayed(loginTime).then((_) {
if (!users.containsKey(data.name)) {
return 'User not exists';
}
if (users[data.name] != data.password) {
return 'Password does not match';
}
return null;
});
}
Future<String> _recoverPassword(String name) {
print('Name: $name');
return Future.delayed(loginTime).then((_) {
if (!users.containsKey(name)) {
return 'User not exists';
}
return null;
});
}
@override
Widget build(BuildContext context) {
return FlutterLogin(
title: 'ECORP',
logo: 'assets/images/ecorp-lightblue.png',
onLogin: _authUser,
onSignup: _authUser,
onSubmitAnimationCompleted: () {
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => DashboardScreen(),
));
},
onRecoverPassword: _recoverPassword,
);
}
}
class _MyHomePageState extends State<MyHomePage> {
SearchBar searchBar;
AppBar buildAppBar(BuildContext context) {
return new AppBar(
title: new Text('My Home Page'),
actions: [searchBar.getSearchAction(context)]
);
}
_MyHomePageState() {
searchBar = new SearchBar(
inBar: false,
setState: setState,
onSubmitted: print,
buildDefaultAppBar: buildAppBar
);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: searchBar.build(context)
);
}
}
void main() => runApp(const MyApp()); // This is the main application widget.
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
int _selectedIndex = 0;
static const List<Widget> _widgetOptions = <Widget>[
Text(
'Reuse',
style: TextStyle(fontSize: 20),
),
Text(
"Reduce",
style: TextStyle(fontSize: 20),
),
Text(
'Recycle',
style: TextStyle(fontSize: 20),
)
];
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Food3Rs'),
backgroundColor: Colors.green,
),
drawer: Drawer(
child: ListView(padding: EdgeInsets.zero, children: const <Widget>[
DrawerHeader(
decoration: BoxDecoration(color: Colors.green),
child: Text(
'Food Reduction',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
ListTile(
leading: Icon(Icons.bookmark),
title: Text('Saved Recipes'),
),
ListTile(
leading: Icon(Icons.account_circle),
title: Text('Profile'),
),
ListTile(leading: Icon(Icons.settings), title: Text('Settings'))
]),
),
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.food_bank_outlined),
label: ('Reduce'),
backgroundColor: (Colors.lightGreen),
),
BottomNavigationBarItem(
icon: Icon(Icons.food_bank),
label: ('Reuse'),
),
BottomNavigationBarItem(
icon: Icon(Icons.cloud),
label: ('Recycle'),
backgroundColor: (Colors.lightGreen),
)
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}
Error messages: The return type 'Null' isn't a 'FutureOr', as required by the closure's context. The return type 'Null' isn't a 'FutureOr', as required by the closure's context. The method 'DashboardScreen' isn't defined for the type 'LoginScreen'. 'dynamic' doesn't conform to the bound 'StatefulWidget' of the type parameter 'T'. The name 'MyHomePage' isn't a type so it can't be used as a type argument. Non-nullable instance field 'searchBar' must be initialized. The declaration '_MyHomePageState' isn't referenced. The library 'package:flutter_search_bar/flutter_search_bar.dart' is legacy, and should not be imported into a null safe library. Typo: In word 'ECORP' Typo: In word 'ecorp' Typo: In word 'lightblue'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
