【问题标题】:Flutter missing implementation for print颤振缺少打印实现
【发布时间】:2018-10-04 13:44:46
【问题描述】:

我开始学习颤振/飞镖,并坚持使用基本打印。

我有一些通过 Android Studio 运行的基本应用程序。

我注意到,我可以在 myLocationApp 类中使用“print”和“debugPrint”,但不能在 myTestLocation 类中使用。

如果我尝试使用

print( myTestString );

错误是

compiler message: lib/main.dart:8:7: Error: The non-abstract class 'MyTestLocation' is missing implementations for these members:
compiler message:   'print'.

当我在线搜索例如here 时,它没有提到任何这些,所以我在这里缺少的主要本质是什么,我猜我需要修改类以包含/引用打印?但不确定最好的方法是什么。


import 'package:flutter/material.dart';
import 'package:location/location.dart';
import 'package:flutter/foundation.dart';

void main() => runApp(new MyLocationApp());

class MyTestLocation { String myTestString = "Testing2"; print( myTestString ); // No implentation error here

}

class MyLocationApp extends StatelessWidget { @override Widget build(BuildContext context) { // TODO: implement build

final location = new MyTestLocation();
debugPrint( location.toString() );

return new MaterialApp(
  title: 'Welcome',
  home: new Scaffold(
    appBar: new AppBar(
      title: new Text('appBarTitle'),
    ),
      body: new Center(
        child: new Text( location.myTestString ),
      )
  ),
);

} }

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    我的理解是,您希望在初始化类时打印“print(myTestString)”(行:最终位置 = new MyTestLocation();)。为此,您必须像这样实现 MyTestLocation 的构造函数:

    class MyTestLocation {
      String myTestString = "Testing2";
    
      MyTestLocation() {
        print(myTestString);
      }
    }
    

    您在任何类的根中编写的代码都有一个声明性函数(不是“运行”的说法)。

    【讨论】:

    • 啊哈是的,我现在明白了,我对课程的误解!现在效果很好。
    【解决方案2】:

    您的课程MyTestLocation 包含错误。基本上用你下面的代码

    class MyTestLocation  {
      String myTestString = "Testing2";
      print( myTestString ); // No implentation error here
    }
    

    您定义了一个类MyTestLocation,其字段myTestString 初始化为Testing2,并定义了一个抽象方法print,其参数名为myTestString。只需删除此打印行,您的应用就可以运行了。

    编辑:修复我造成的混乱 - 抱歉

    【讨论】:

    • 是的,我知道那里有一个错误,因此它旁边的注释,但我试图找到如何从该类中打印一些东西(即用于调试)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2020-11-29
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多