Home > other >  flutter : unable to forward BluetoothDevice to next page
flutter : unable to forward BluetoothDevice to next page

Time:01-20

I try to connect to a bluetooth device, I manage to list and retrieve the addresses. Thanks to a button, I want to go to a next page and transfer the information of the selected bluetooth device. But when I print I get an error message:

'print' must have a method body because '_HomeappiState' isn't abstract.
Try making '_HomeappiState' abstract, or adding a body to 'print'.

I used the same type of constructor but it doesn't work. Error message and unable to retrieve variable. I tried to transfer just a String but without success. I go well to the second page when I do not transfer data

Here is the code for my button :

 onTap: () {
          Navigator.of(context).push(MaterialPageRoute(
              builder: (context) => Homeappi(serverlist: serverlist)));
        },

Code of the page that receives the variable:

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';

class Homeappi extends StatefulWidget {
  final BluetoothDevice serverlist;
  const Homeappi({Key? key, required this.serverlist}) : super(key: key);

  @override
  _HomeappiState createState() => _HomeappiState();
}

class _HomeappiState extends State<Homeappi> {
  print(serverlist);
  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    return Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: Colors.transparent,
        appBar: AppBar(
          title: const Text('Connexion'),
          backgroundColor: Colors.transparent,
          elevation: 0.0,
        ),
        body: Container(
            alignment: Alignment.topCenter,
            decoration: BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
                  Colors.blue,
                  Colors.greenAccent.shade400,
                  Colors.blue,
                ],
              ),
            ),
            child: const Text('Test Test')));
  }
}

I can't find my mistake

thank you

CodePudding user response:

You can't use print without a method. I suggest create an initState method and when compiler reach Homeappi the print will work.

Do like this:

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';

class Homeappi extends StatefulWidget {
  final BluetoothDevice serverlist;
  const Homeappi({Key? key, required this.serverlist}) : super(key: key);

  @override
  _HomeappiState createState() => _HomeappiState();
}

class _HomeappiState extends State<Homeappi> {
  

   void initState() {
    // TODO: implement initState
    super.initState();
    print(widget.serverlist); // instead of print(serverlist)
  }
  @override
  Widget build(BuildContext context) {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    return Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: Colors.transparent,
        appBar: AppBar(
          title: const Text('Connexion'),
          backgroundColor: Colors.transparent,
          elevation: 0.0,
        ),
        body: Container(
            alignment: Alignment.topCenter,
            decoration: BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
                  Colors.blue,
                  Colors.greenAccent.shade400,
                  Colors.blue,
                ],
              ),
            ),
            child: const Text('Test Test')));
  }
}
  •  Tags:  
  • Related