Home > Net >  I want to make fibonacci series infinite
I want to make fibonacci series infinite

Time:01-02

hello guys this program display Fibonacci series in list views but it display first two numbers 1 -1 only in Fibonacci series the number equal the sum of the last two numbers example 5 equal 3 2 and 8 equal 3 5 I want program display infinite Fibonacci series what shall I do to make the list infinite Fibonacci series ? there is the code to know the cause of displaying 1-1 only in the list

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

display the Fibonacci series 0,1,1,2,3,5,8,13 and so on.

CodePudding user response:

The only issue with your code is you are providing length of list i.e. numbers.cache.length. As @mmcdon20 mentioned in comment, if you remove it, your code will run fine as it is.

There is another issue in your solution. As the snackbar message should consider that list index start with 0 but the real world sequence starts with index 1.

Modified your code a little bit

import 'package:flutter/material.dart';

void main(args) {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(useMaterial3: true),
      home: const FibonacciSequencePage(),
    );
  }
}

class FibonacciNumbers {
  FibonacciNumbers();

  final cache = {0: BigInt.from(1), 1: BigInt.from(1)};

  BigInt get(int i) {
    if (!cache.containsKey(i)) {
      return cache[i] = get(i - 1)   get(i - 2);
    }

    return cache[i]!;
  }
}

class FibonacciSequencePage extends StatelessWidget {
  const FibonacciSequencePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Fibonacci Sequence'),
      ),
      body: FibonacciSequenceListView(numbers: FibonacciNumbers()),
    );
  }
}

class FibonacciSequenceListView extends StatelessWidget {
  final FibonacciNumbers numbers;

  const FibonacciSequenceListView({super.key, required this.numbers});

  @override
  Widget build(BuildContext context) {
    return ListView.builder(itemBuilder: (context, index) {
      return ListTile(
        title: Text(numbers.get(index).toString()),
        onTap: () {
          final snack = SnackBar(
            content: Text(
                '${numbers.get(index)} is #${index   1} in the Fibonacci sequence!'),
          );
          ScaffoldMessenger.of(context).showSnackBar(snack);
        },
      );
    });
  }
}

CodePudding user response:

You need to edit many things. I edited your code. This is 100% working solution.

import 'package:flutter/material.dart';

void main() async {
  final numbers = FibonacciNumbers();

  runApp(
    MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.black,
          title: const Text('Fibonacci List'),
        ),
        body: FibonacciListView(numbers),
      ),
    ),
  );
}

class FibonacciNumbers {
  final fibList = [BigInt.from(1), BigInt.from(1)];

  void addElementsToList(int n) {
    print("elements are added");
    for (int i = 0; i < n; i  ) {
      fibList.add(
          fibList.last   fibList[fibList.length - 2]);
    }
  }
}

class FibonacciListView extends StatefulWidget {
  final FibonacciNumbers numbers;
  const FibonacciListView(this.numbers, {super.key});

  @override
  State<FibonacciListView> createState() => _FibonacciListViewState();
}

class _FibonacciListViewState extends State<FibonacciListView> {
  final scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
    widget.numbers.addElementsToList(20);
    scrollController.addListener(_listenerMethod);
  }

  void _listenerMethod() {
    if (scrollController.position.maxScrollExtent -
            scrollController.position.pixels <=
        100) {
      setState(() {
        widget.numbers.addElementsToList(20);
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: widget.numbers.fibList.length,
      controller: scrollController,
      itemBuilder: (context, i) {
        return ListTile(
          title: Text('${widget.numbers.fibList[i]}'),
          onTap: () {
            final snack = SnackBar(
              content: Text('${widget.numbers.fibList[i]} is #$i in the Fibonacci sequence!'),
            );
            ScaffoldMessenger.of(context).showSnackBar(snack);
          },
        );
      },
    );
  }
}

By the way, I deleted an app bar and a scaffold object. Because you put the Scaffold inside another Scaffold in you code in question.

  • Related