通过更新Flutter 3.8.0,在flutter_date_time_picker中出现错误。

huangapple go评论72阅读模式
英文:

By updating Flutter 3.8.0 Error in flutter_date_time_picker

问题

通过更新 Flutter 3.8.0,我在 flutter_date_time_picker 中遇到了以下错误。我想知道是否有人知道如何解决这个问题。

../../../../.pub-cache/git/flutter_datetime_picker-eb66486c47d50bf550950c196486121ffcea8885/lib/flutter_datetime_picker.dart:7:1: 错误:
'DatePickerTheme' 从 'package:flutter/src/material/date_picker_theme.dart' 和 'package:flutter_datetime_picker/src/datetime_picker_theme.dart' 都有导入。

pubspec.yaml

  flutter_datetime_picker:
    git:
      url: https://github.com/Realank/flutter_datetime_picker.git

如果有人知道解决方案,我很愿意听听。我认为这可能是由于 Flutter 的更新,因为直到昨天我都可以正常构建!如果有人知道解决方案,我很愿意听听。

英文:

By updating Flutter 3.8.0
I am getting the following error in flutter_date_time_picker.
I would like to know if anyone knows a solution to this problem.

../../../../.pub-cache/git/flutter_datetime_picker-eb66486c47d50bf550950c196486121ffcea8885/lib/flutter_datetime_picker.dart:7:1: Error:
'DatePickerTheme' is imported from both 'package:flutter/src/material/date_picker_theme.dart' and'package:flutter_datetime_picker/src/datetime_picker_theme.dart'.

pubspeck.yaml

  flutter_datetime_picker:
    git:
      url: https://github.com/Realank/flutter_datetime_picker.git

If anyone knows of a solution, I would love to hear about it.

I believe this is probably due to the flutter update, as I was able to build normally until yesterday!
If anyone knows of a solution, I would love to hear about it.

答案1

得分: 11

你可以使用

https://pub.dev/packages/flutter_datetime_picker_plus

它是从(Pub) flutter_datetime_picker分叉而来的,因为它在dart 3.0中存在问题。

英文:

you may use

https://pub.dev/packages/flutter_datetime_picker_plus

It is a Forked from (Pub) flutter_datetime_picker as it had issues with dart 3.0

答案2

得分: 2

Name Clash

这个错误:

'DatePickerTheme' 从两个不同的包中导入

告诉我们当前文件中从不同包中导入的两个类具有相同的名称。

Dart 无法确定要使用哪一个,因此出现错误。

两个常见解决方案

  1. hide(不要导入)其中一个类
  2. 为其中一个包应用前缀

Hide

下面是一个隐藏一个包中单个类的示例:

import 'package:text_widgets/text_widgets.dart' hide TextThemeExt;

上面的 TextThemeExt 类将不会被导入,并且不会与当前文件中同名的类冲突。

Prefix

import 'dart:math' as math;

这是在 Dart 中应用到 math 库的一个非常常见前缀。这样阅读代码的人可以理解这些符号来自于 math 库,而不是当前文件中的某个缩写变量名。

现在,math 库中的任何符号都必须以 math 为前缀才能使用。

例如:

Matrix4.rotationX(math.pi)

如果在导入语句中没有前缀,上述代码将是:

Matrix4.rotationX(pi)

摘要

在这种特定情况下,包的作者选择了一个与 Flutter Material 包中的一个类的名称冲突的名称,这是大多数 Flutter 开发者必不可少的包。

建议的修复中,有人提议将 Flutter 的 DatePickerTheme 类从导入中隐藏。

如果您正在使用这个作者的包,除了等待他们合并更改并发布新版本的包之外,您没有太多需要做的。

英文:

Name Clash

This error:

'DatePickerTheme' is imported from both

is telling us two classes imported into the current file from different packages have the same name.

Dart cannot tell which to use, hence the error.

Two common solutions

  1. hide (don't import) one of the classes
  2. Apply a prefix to one of the packages

Hide

Here's an example of hiding a single class from being imported from a package:

import 'package:text_widgets/text_widgets.dart' hide TextThemeExt;

Above the class named TextThemeExt won't be imported and won't clash with an identically named class in the current file.

Prefix

import 'dart:math' as math;

This is a very common prefix applied to the math library in dart. (So people reading the code understand these are symbols from the math library and not some short variable name in the current file.)

Any symbol within the math library now must be prefixed with math to be used.

For example:

Matrix4.rotationX(math.pi)

Without the prefix in the import statement the above would be:

Matrix4.rotationX(pi)

Summary

In this particular case, the author of the package chose a name for a class they created that conflicts with the name of a class in the Flutter Material package, which is a pretty essential package for most Flutter devs.

In the proposed fix, a person offered to hide Flutter's DatePickerTheme class from being imported.

If you're using this package from this author, there's not much for you to do besides wait for them to merge the change and publish a new version of their package.

答案3

得分: 2

使用别名导入包对我有效。我在同一个Dart文件中使用了“material”和“flutter_datetime_picker”。

英文:

using an alias on importing package works for me. I'm using material an flutter_datetime_picker in same dart file.

答案4

得分: 2

我遇到了相同的问题,是由于Material和flutter_datetime_picker共享名称导致的。可以通过给导入添加别名然后指定使用的插件来解决该问题:

import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart' as PluginDatetimePicker;

然后,在代码中调用对象时,必须指定它来自PluginDatetimePicker:

PluginDatetimePicker.DatePicker.showDatePicker(
     context,
     locale: PluginDatetimePicker.LocaleType.en,
     theme: PluginDatetimePicker.DatePickerTheme(
            headerColor: headerColorX,
            backgroundColor: backgroundColorX,
            itemStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 22),
            doneStyle: TextStyle(color: Colors white, fontSize: 16),
     ), 
     onConfirm: (date) {
         // onConfirm()
     });

希望对你有所帮助。

英文:

I had the same issue caused by Material and flutter_datetime_picker sharing names. It can be solved by adding an alias to your import then specifying which plugin is being used:

import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart' as PluginDatetimePicker;

Then when calling your objects in your code you must specify that it is coming from PluginDatetimePicker:

PluginDatetimePicker.DatePicker.showDatePicker(
     context,
     locale: PluginDatetimePicker.LocaleType.en,
     theme: PluginDatetimePicker.DatePickerTheme(
            headerColor: headerColorX,
            backgroundColor: backgroundColorX,
            itemStyle: TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 22),
            doneStyle: TextStyle(color: Colors.white, fontSize: 16),
     ), 
     onConfirm: (date) {
         // onConfirm()
     });

答案5

得分: 2

你可以使用这个包 "flutter_datetime_picker_plus"。

https://pub.dev/packages/flutter_datetime_picker_plus/install

它的使用方式与 flutter_datetimer_picker 完全相同,只需安装即可。

英文:

you can also use this package "flutter_datetime_picker_plus"

https://pub.dev/packages/flutter_datetime_picker_plus/install

it works exactly like flutter_datetimer_picker you just have to install and that's all.

答案6

得分: 0

首先尝试运行 flutter clean,然后运行 pub get 并重新安装您想要的包。如果再次出现此错误,请尝试以下步骤:

要解决这个问题,在包含两个导入的类中,使用 "as" 关键字为其中一个包分配一个别名。

import 'package:flutter/src/material/date_picker_theme.dart' as dp
dp.EveryMethodYouWant(); // 使用别名调用该类

如果您有任何问题,我在这里。

愉快的编码!

英文:

first try to flutter clean and then pub get and reinstall the package you want. if again you had this error try this:
To resolve it; in the class where you have both imports, assign an alias to one of the packages using the as keyword.

import 'package:flutter/src/material/date_picker_theme.dart' as dp
dp.EveryMethodYouWant(); //call the class using the alias

if you had any question i'm here.

happy coding.

答案7

得分: 0

你误导入了错误的包,因为根据包的文档,没有提到这样的代码行。

导入下面提到的包,然后尝试运行 "pub get"。

import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';

一切将正常运作。

英文:

By mistakenly,you have imported the wrong package because as per the package documentation, there is no such line mentioned.

Import the package mentioned below and try pub get.

import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';

everything vill works fine.

huangapple
  • 本文由 发表于 2023年2月10日 12:54:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75407100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定