flutter 无法在此处使用前缀“cellphoneOfRafa”,因为它被局部声明隐藏,请尝试重命名前缀或局部声明

wixjitnu  于 2023-01-09  发布在  Flutter
关注(0)|答案(1)|浏览(114)

好吧,我在 dart 乞讨,我正在努力解决这个问题,有人能帮我解决吗?

Celular cellphoneOfRafa = Celular('Azul', 5, 0.500, 5.7);
Celular cellphoneOfZe = Celular('Preto', 6, 0.700, 7.7);

  print(cellphoneOfRafa.toString());
  print(cellphoneOfZe.toString());
  
  double resultado = cellphoneOfRafa.valorDoCelular(1000);
  print(resultado);

  class Celular {
    final String cor;
    final int qtdPros;
    final double tamanho;
    final double peso;

  Celular(this.cor, this.qtdPros, this.peso, this.tamanho);

  String toString() {
    return 'cor $cor, Quantidade de Processadores $qtdePros';
  }

  double valorDoCelular(double valor) {
    return valor * qtdPros;
  }
}

我试着把块代码放在主类中,但效果不好。

7d7tgy0s

7d7tgy0s1#

  • dart不允许匿名类,你完全可以把它放在主体之外。
import 'package:flutter/material.dart';

class MyWidget extends StatefulWidget {
  MyWidget({Key? key}) : super(key: key);

  @override
  State<MyWidget> createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  void test() {
    Celular cellphoneOfRafa = Celular('Azul', 5, 0.500, 5.7);
    Celular cellphoneOfZe = Celular('Preto', 6, 0.700, 7.7);
    print(cellphoneOfRafa.toString());
    print(cellphoneOfZe.toString());
    double resultado = cellphoneOfRafa.valorDoCelular(1000);
    print(resultado);
  }

  @override
  Widget build(BuildContext context) {
    test();
    return Scaffold();
  }
}

class Celular {
  final String cor;
  final int qtdPros;
  final double tamanho;
  final double peso;

  Celular(this.cor, this.qtdPros, this.peso, this.tamanho);

  String toString() {
    return 'cor $cor, Quantidade de Processadores ${qtdPros}';
  }

  double valorDoCelular(double valor) {
    return valor * qtdPros;
  }
}

相关问题