#JsonKey Arguments of a constant creation must be constant expressions - flutter

i am trying to make MainResponse class which is returned from backend with same format. I have created a Data class and the object it has should be generic json key regarding the class name like below:
import 'package:json_annotation/json_annotation.dart';
import 'package:style/api/response/status_response.dart';
part 'data.g.dart';
#JsonSerializable(genericArgumentFactories: true)
class Data<T> {
#JsonKey(name: T.toString())
T? returnedObject;
}
what I wanted to do is giving JsonKey name value as class name. but i am receiving following error:
Arguments of a constant creation must be constant expressions.
is there a way to achieve it?

sorry, the question was easy. just try to edit classname.g.dart file which is auto generated. and use'T.toString().toLowerCase()'instead of auto generated key.

Related

how to write a meaningful test for `extending a class`?

what is the correct way to write a meaningful test for extending a class with the super keyword?
class FooBase<T> {
final T data;
const FooBase(this.data);
}
class Foo<T> extends FooBase<T> {
const Foo(T data)
: super(data);
}
I suppose super(data) ist passing the constructor parameter to the constructor of the base class (I do not know a lot about dart).
You can only test if super was called by observing its behavior. You have to check the base classes constructor for what it is doing with the parameter and then see if you can observe if that happened (if possible). Example: If the base class constructor is assigning the parameter to a field that you can access from the outside, you can assert on that parameter being set. This only works if
You can access the field from your test code. Access in this context does not mean to access it directly or via a getter. Any means of observing that the field has been set should be sufficient.
The class under test is not setting the field itself (there is probably no way to decide which constructor set the field).

Is there a way to access constants of a (java) class by an alias in scala?

I would like to make a short alias for a java class.
Is there a way to import (or make a type alias to) a java class (e.g. HttpServletResponse), and access its constant values (e.g. HttpServletResponse) using the alias?
Type alias works fine, but I cannot find a way to access the constants of the class.
EDIT:
I'm sorry I asked a wrong question. I knew importing with a short name works.
What I like to do is avoid writing import ...{HttpServletResponse => Response} every file in which MyHttpServlet is mixed-in.
Type aliases in MyHttpServlet makes it possible without importing, but accessing constants is still the issue. (or maybe it's impossible?)
trait MyHttpServlet extends HttpServlet {
import javax.servlet.http.HttpServletResponse
// works
type Response = HttpServletResponse
// compile error: object creation impossible, since it has 36 unimplemented members.
//object Response extends HttpServletResponse
def notAllowed(response: Response): Unit = {
// works
response.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED)
// I would like to do something like this
//response.setStatus(Response.SC_METHOD_NOT_ALLOWED)
}
}
Just rename the import.
import javax.servlet.http.{HttpServletResponse => Response}
response.setStatus(Response.SC_METHOD_NOT_ALLOWED)
Type aliases don't allow static member access because one can't call members on a type in scala, it is only possible on values.

µPickle Couldn't derive type error on write

I'm trying to use µPickle in Scala.js to produce Json for Ajax request.
Here is my code:
import upickle._
import upickle.default._
case class FmData(name: String, comment: String)
val data = write(FmData("name", "comment"))
And I get error:
Couldn't derive type FmData
How come?
The same issue happened to me before. It was happening when I was defining this case class in package object, other object or inside of method. However when I extracted it to a separate file, it was all fine and working.
Hope it helps.

How to mock case class?

I have following case class:
case class User(username:String, createdDate:DateTime)
and a class to test:
class UserDAO{
def registerUser(user:User)
}
I want to verify that registerUser called with an input user that has username="myusername". Based on this docs https://mockito.googlecode.com/hg-history/1.7/javadoc/org/mockito/Matchers.html I came up with a code like this:
verify(userDAO).registerUser(User(eq("myusername"),any[DateTime]))
however it is not right and I had error while compiling.
Error is Type mismatch, expected UUID actual Boolean
I solved my problem by using a different approach. I used capture the input argument of a mocked object and then validate the captured value.
http://docs.mockito.googlecode.com/hg/org/mockito/ArgumentCaptor.html

Hiding an import from TypeScript "prelude"

If I try to define a class with the same name as a type which is imported automatically by TypeScript, such as
class Map {
}
then I get the following error
error TS2000: Duplicate identifier 'Map'.
What I would like to do is to be able to rename, or avoid importing entirely, the TypeScript library class Map, so that I can define my own with the same name.
Putting my Map in a module (as per one of the answers below) helps, but I still can't refer to it by unqualified name (i.e. by importing), although this time there is no complaint about duplicate names; the import simply doesn't do anything:
Suppose A.ts contains:
module A {
export class Map {
}
}
and B.ts contains:
/// <reference path='A.ts'/>
import Map = A.Map
function test (m: Map) {
}
In order to make this compile I need to replace m: Map by m: A.Map. Otherwise the compiler complains that I'm missing some generic arguments, because it assumes I mean the Map type from the TypeScript "prelude".
I feel like I should be able to define a "local" name (via an explicit declaration, or via an import) which hides any equivalently-named definition in the prelude; or, I should be able to manually disable the importing of particular types from the prelude (as I can in Haskell).
Put your class inside a module..
module SomeNamespace {
class Map {
}
}
This will make your class unique from the default Map class.