i have a program make in emulator of flutter application unexpected null value eventhough it doesn't making errors in analyzer it is fibonacci series i don't know why making error in emulator
i write this code according to null safety
import 'package:flutter/material.dart';
void main() async {
final numbers = FibonacciNumbers();
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text('Fibonacci List'),
),
body: FibonacciListView(numbers),
),
),
);
}
class FibonacciNumbers {
final cache = {0: BigInt.from(1), 1: BigInt.from(1)};
BigInt get(int i) {
if (!cache.containsKey(i)) {
cache[i] = get(i - 1) get(i - 2);
}
return cache['$i']!;
}
}
class FibonacciListView extends StatelessWidget {
//static const route ='/pagetwo';
FibonacciNumbers? numbers;
FibonacciListView(this.numbers);
@override
Widget build(BuildContext context) {
// Navigator.pushNamed(context,FibonacciListView.route);
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: Text('Fibonacci List'),
),
body: ListView.builder(
itemCount: numbers!.cache.length,
itemBuilder: (context, i) {
return ListTile(
title: Text('${numbers!.get(i)}'),
onTap: () {
final snack = SnackBar(
content: Text('${numbers!.get(i)} is '
'#$i in the Fibonacci sequence!'),
);
Scaffold.of(context).showSnackBar(snack);
},
);
},
));
}
}
CodePudding user response:
Your error comes from this line:
return cache['$i']!;
You declared cache
as a Map
of <int, BigInt>
. To access the value, you should return:
return cache[i]!;
I also replaced the display of snackbar, since showSnackBar
is deprecated for class Scaffold
.
Here is the updated code:
import 'package:flutter/material.dart';
void main() async {
final numbers = FibonacciNumbers();
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: Text('Fibonacci List'),
),
body: FibonacciListView(numbers),
),
),
);
}
class FibonacciNumbers {
final cache = {0: BigInt.from(1), 1: BigInt.from(1)};
BigInt get(int i) {
if (!cache.containsKey(i)) {
cache[i] = get(i - 1) get(i - 2);
}
return cache[i]!;
}
}
class FibonacciListView extends StatelessWidget {
final FibonacciNumbers numbers;
const FibonacciListView(this.numbers, {super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: Text('Fibonacci List'),
),
body: ListView.builder(
itemCount: numbers.cache.length,
itemBuilder: (context, i) {
return ListTile(
title: Text('${numbers.get(i).toString()}'),
onTap: () {
final snack = SnackBar(
content: Text('${numbers.get(i)} is #$i in the Fibonacci sequence!'),
);
ScaffoldMessenger.of(context).showSnackBar(snack);
},
);
},
),
);
}
}
CodePudding user response:
This seems problematic, to me:
final cache = {
0: BigInt.from(1),
1: BigInt.from(1)
};
BigInt get(int i) {
if (!cache.containsKey(i)) {
cache[i] = get(i - 1) get(i - 2);
}
return cache['$i']!;
}
You're trying to add entries to the Map cache
if they don't already exist, right? But cache
is declared as final! You can't add any entries to it!
Also this looks dangerous:
itemCount: numbers!.cache.length,
This is in a StatelessWidget!... Will the item count really update when the length of cache changes?
In the normal case, I would use a StatelessWidget for this, and then call setState() every time the length of cache changes...