skip to Main Content

I’m trying to send a list from one page to another page in Flutter. I was able to see my list in Text first, but I couldn’t add it to a Wheel of Fortune using flutter_fortune_wheel 1.2.I couldn’t understand if the problem is in the package or the code I wrote, can you help me?

Main

import 'package:flutter/material.dart';
import 'MainScreen.dart';

void main() => runApp(MyApp());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MainScreen(),
    );
  }
}

MainScreen

import 'package:flutter/material.dart';
import 'SpinningWheelScreen.dart';

class MainScreen extends StatefulWidget {
  @override
  State<MainScreen> createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {
  TextEditingController t1 = TextEditingController();
  void add() {
    setState(() {
      myList.add(t1.text);
      t1.clear();
    });
  }

  final myList = <String>[];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Main Screen'),
        centerTitle: true,
      ),
      body: Center(
        child: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              Text("Entering Page"),
              TextFormField(
                controller: t1,
              ),
              ElevatedButton(onPressed: add, child: Text('Add')),
              ElevatedButton(
                  onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => SpinningWheelScreen(
                                inputs: [myList],
                              )),
                    );
                  },
                  child: Text('Next'))
            ],
          ),
        ),
      ),
    );
  }
}

SpinningWheelScreen

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_fortune_wheel/flutter_fortune_wheel.dart';

class SpinningWheelScreen extends StatefulWidget {
  var inputs = [];

  SpinningWheelScreen({required this.inputs});

  @override
  State<SpinningWheelScreen> createState() => _SpinningWheelScreenState();
}

class _SpinningWheelScreenState extends State<SpinningWheelScreen> {
  StreamController<int> selected = StreamController<int>();

  void dispose() {
    selected.close;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('WheelSpin App'),
        centerTitle: true,
      ),
      body: Center(
        child: Container(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              GestureDetector(
                onTap: () {
                  setState(() {
                    selected.add(Fortune.randomInt(0, widget.inputs.length));
                  });
                },
              ),
              Expanded(
                child: FortuneWheel(
                  animateFirst: false,
                  selected: selected.stream,
                  items: [
                    for (var it in widget.inputs) FortuneItem(child: Text(it)),
                  ],
                ),
              ),
              Text(widget.inputs.toString()),
              ElevatedButton(
                  onPressed: () {
                    Navigator.pop(context);
                  },
                  child: Text('Go Back')),
            ],
          ),
        ),
      ),
    );
  }
}

I would like to route myList to another screen

2

Answers


  1. The error occurs because you are passing a List<List> to your SpinningWheelScreen

    MaterialPageRoute(
      builder: (context) => SpinningWheelScreen(
                              inputs: [myList], // myList is already a List
                             ),
    ),
    

    To fix your error do the following:

    MaterialPageRoute(
      builder: (context) => SpinningWheelScreen(
                              inputs: myList, // Remove the brackets
                             ),
    ),
    

    Now you are passing just a List to SpinningWheelScreen.

    Login or Signup to reply.
  2. You have to add one more for loop in FortuneWheel ‘s items in SpinningWheelScreen .

    items: [
          for (var i in widget.inputs)
              for (var j = 0; j < i.length; j++)
                  FortuneItem(child: Text(i[j])),
     ],
    

    Also you have to remove GestureDetector above your expanded widget or add child to it. because it is causing issue. After removing GestureDetector it works fine.

    Result:

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search