最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dart - Flutter screen parameter key moving from one screen to other - Stack Overflow

programmeradmin4浏览0评论
onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => ProtocolListScreen()),
                    );
                  },

//ProtocolListScreen need a parameter to move from home page to that screen but i don't want to as i have to put that key in every screen which is making difficulties

class ProtocolListScreen extends StatelessWidget {
  final BluetoothDevice polarDevice;
  final BluetoothDevice breathingDevice;
  // Constructor to accept Bluetooth devices
   ProtocolListScreen({
    Key? key,
    required this.polarDevice,
    required this.breathingDevice,
  }) : super(key: key);

trailing: TextButton(
                onPressed: () {
                  if (protocol['name'] == 'Anulom Vilom Pranayam') {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => AnulomVilomScreen(
                          polarDevice: polarDevice,  // ✅ Pass the device properly
                          breathingDevice: breathingDevice, // ✅ Pass the device properly
                        ),
                      ),
                    );
                  }
}
onPressed: () {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => ProtocolListScreen()),
                    );
                  },

//ProtocolListScreen need a parameter to move from home page to that screen but i don't want to as i have to put that key in every screen which is making difficulties

class ProtocolListScreen extends StatelessWidget {
  final BluetoothDevice polarDevice;
  final BluetoothDevice breathingDevice;
  // Constructor to accept Bluetooth devices
   ProtocolListScreen({
    Key? key,
    required this.polarDevice,
    required this.breathingDevice,
  }) : super(key: key);

trailing: TextButton(
                onPressed: () {
                  if (protocol['name'] == 'Anulom Vilom Pranayam') {
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => AnulomVilomScreen(
                          polarDevice: polarDevice,  // ✅ Pass the device properly
                          breathingDevice: breathingDevice, // ✅ Pass the device properly
                        ),
                      ),
                    );
                  }
}
Share Improve this question asked Feb 7 at 12:32 Aditya RanjanAditya Ranjan 1 New contributor Aditya Ranjan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1
  • 1 Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Feb 7 at 13:11
Add a comment  | 

1 Answer 1

Reset to default 0

Your question is not clear, but I will give you a solution from the provided info.

You use provider package, You can create a dart class like this.

import 'package:flutter/material.dart';

class BluetoothDeviceHelper extends ChangeNotifier {
  late BluetoothDevice _polarDevice;
  late BluetoothDevice _breathingDevice;

  // Getters
  BluetoothDevice get polarDevice => _polarDevice;
  BluetoothDevice get breathingDevice => _breathingDevice;

  // Setters
  void setPolarDevice(BluetoothDevice value) {
    polarDevice = value;
    notifyListeners();
  }
  
  void setBreathingDevice(BluetoothDevice value) {
    breathingDevice = value;
    notifyListeners();
  }
}

IMPORTANT NOTES:

  • Make sure you put your provider class above the screens in the widget tree (if you want to use it all over the app, it is better to put above MaterialApp() ) like this.
ChangeNotifierProvider(
  create: (BuildContext context) => BluetoothDeviceHelper(),
  child: MaterialApp(),
),

and you can use the values you want this way:

 Provider.of<BluetoothDeviceHelper>(context, listen: false).polarDevice;
 Provider.of<BluetoothDeviceHelper>(context, listen: false).breathingDevice;
发布评论

评论列表(0)

  1. 暂无评论