abdelrahmandarrage
abdelrahmandarrage

Reputation: 61

Emulator app throws unexpected null value

I have a program run on emulator of flutter application. It throws the error "unexpected null value" even though 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);
              },
            );
          },
        ));
  }
}

Upvotes: 0

Views: 177

Answers (2)

Karolina Hagegård
Karolina Hagegård

Reputation: 1387

This seems problematic, to me:

itemCount: numbers!.cache.length,

If you wanted the item count to update when the length of cache changes, it won't. You will get only those two first items that correspond to the original length of cache.

If you want an infinite ListView of fibonacci numbers, however, you can just remove the itemCount property from the ListView.builder() altogether! This will make the builder generate as many List elements as it can fit on the screen, plus a few more to be prepared for when the user scrolls. Then, as they scroll, more elements will be generated eternally.

That seems like a nice solution, if that's what you wanted! 🙂 Otherwise, I agree with Lucie's excellent answer.

Upvotes: 0

lucie
lucie

Reputation: 1020

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);
            },
          );
        },
      ),
    );
  }
}

Upvotes: 2

Related Questions